Tuesday, 19 September 2017

Java Program to find largest digit in the given input and calculate the multiples of the largest digit with all individual digits to single digit

Question:

Program to find largest digit in the given input and calculate the multiples of the largest digit with all individual digits to single digit

Input: 123
Output: Result: 9

Program:

import java.io.*;

public class LetUsCodeinJava{
     public static void main(String[ ] args){
          String inpStr = "123";
          int res = 0, sum = 0;
          int maxDigit = Integer.parseInt(inpStr.charAt(0) + "");
          // to find max digit in the given string
          for(int i = 0; i < inpStr.length( ); i++){
               if(Integer.parseInt(inpStr.charAt(i) + "") > maxDigit){
                    maxDigit = Integer.parseInt(inpStr.charAt(i) + "");
               }
          }
          // multiples of the largest digit with all individual digits
          for(int i = 0; i < inpStr.length( ); i++){
               res += maxDigit * Integer.parseInt(inpStr.charAt(i) + "");
          }
          // sum of digits to single digit
          while(res > 0 || sum > 9){
               if(res == 0){
                     res = sum;
                     sum = 0;
               }
               sum += res % 10;
               res /= 10;
           }
           System.out.println("Result: "sum);
     }
}

Output:

Result: 9

Explanation:

We need to divide the program into 3 section
1. to find max digit in the given string.
2. multiples of the largest digit with all individual digits.
3. sum of resultant digits to single digit.

In the 1st section we will get the largest digit

maxDigit = 3:

In the 2nd section we need to multiply all the digits with maxDigit

so, 1 * 3 = 3, 2 * 3 = 6, 3 * 3 = 9

res = 3 + 6 + 9 = 18

In the 3rd section we are calculating the sum of digits into single digit

so, sum = 1 + 8 = 9

Hence, Result = 9.



Monday, 18 September 2017

Java Program to remove special characters and print sum of the unique numerical characters in the given string


Question:

Program to remove special characters and print sum of print sum of the unique numerical characters in the given string

Input: 123%#$.257
Output: Sum: 18

Program:

import java.io.*;

public class LetUsCodeinC{
     public static void main(String[ ] args){
           String inpStr = "123%#$.257";
           //Removing special characters from the given string
           inpStr = inpStr.replaceAll("[^0-9]","");
           String resStr = "";
           int sum = 0;
           //Removing duplicates from the resStr String
           for(int i = 0; inpStr.length( ); i++){
                if(!resStr.contains(String.valueOf(inpStr.charAt(i)))){
                     resStr += String.valueOf(inpStr.charAt(i));
                }
           }
           //Sum of all the numeric characters of resStr.
           for(int i = 0; resStr.length( ); i++){
                sum += Integer.parseInt(resStr.charAt(i) + "");
           }
           System.out.println("Sum: " + sum);
     }
}

Output:

Sum: 18

Explanation:

First we need to divide this program into 3 modules.
    1. Removing special characters from the given string.
    2. Removing duplicates from the resultant string.
    3. Sum of all the resultant numeric characters of string.




Java Program to remove duplicate characters in the given string


Question:

Program to remove duplicate characters in the given string
Input: 123346778
Output: 12345678

Program:

import java.io.*;

public class LetUsCodeinJava {
      public static void main(String[ ] args){
            String inpStr = "123346778";
            String resStr = "";
            for(int i = 0; i < inpStr.length( ); i++){
                 if(!resStr.contains(String.valueOf(inpStr.charAt(i)))){
                       resStr += String.valueOf(inpStr.charAt(i));
                 }
            }
            System.out.println(resStr);
      }
}

Output:

12345678



Java Program to remove special characters in the given String


Question:

Program to remove special characters in the given string
Input: 1746%$10.
Output: 174610

Program:

import java.io.*;
public class LetUsCodeinJava{
      public static void main(String[ ] args){
           String inpStr = "1746%$10.";
           inpStr = inpStr.replaceAll("[^0-9]","");
           System.out.println(inpStr);
      }
}

Output:

174610

Explanation:

In Java we have replaceAll( ) method to replace the sequence of matching characters with the matching regular expression and we can replace the string with whatever string.

  Syntax:-

  public String replaceAll(String regExpression, String repString)