Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts

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)




Tuesday, 14 March 2017

Java Program on String Manipulations




Write a JAVA program to print following output?

Input Format:

tech prompt

Output Format:

TECH PROMPT
Tech Prompt

Program:

import java.io.*;
import java.lang.*;
public class Test{
   public static void main(String args[ ]){
      String inp = "tech prompt";
      String a[ ] = inp.split(" ");
      System.out.println(inp.toUpperCase( ));
      for(int i = 0;i <  a.length;i++){
            String str = a[i];
            System.out.print((str.substring(0,1).toUpperCase( ) + str.substring(1, str.length( )));
      }
   }
}












Wednesday, 11 May 2016

JAVA Program for printing the triangle pattern


Input Format:

Enter N: 5

Output Format:

*
* *
* * *
* * * *
* * * * *
* * * *
* * * 
* * 


Program:

import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter N: ");
int height = ( Integer.parseInt( s.nextLine( ) ) );
StringBuffer str = new StringBuffer( );
for(int i = 0;i < height*2; i++){
      if( i > = height )
            str.deleteCharAt( 0 );
      else
            str.append("*");
      System.out.println(str);
}
}
}


Monday, 9 May 2016

JAVA program for printing the Capital letters in the given sentence


Explanation:

User give an input as a sentence. That contains set of words separated by space. We have to print the Capital letters in the given input sentence.

Input format:

Enter any sentence:
Let Us Code in C Let Us Code in JAVA

Output format:

L U C C L U C J A V A

Program:

import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any Sentence");
String ss = s.nextLine();
String[ ] a = ss.split(" ");
String res = "";
for(int i = 0;i<a.length;i++)
{
String str = a[i];
for(int j = 0;j<str.length();j++)
{
int ch = str.charAt(j);
if(ch<=90 && ch>=65)
{
res+ = str.charAt(j)+" ";
}
}
}
System.out.println(res);
}
}


Source Code: download

Sunday, 8 May 2016

JAVA program for reversing the words in the given sentence


Explanation:

User give an input as a sentence. That contains set of words separated by space. We have to reverse the each word in the given input sentence.

Input format:

Enter any sentence:
Let Us Code in C Let Us Code in JAVA

Output format:

teL sU edoC ni C teL sU edoC ni AVAJ

Program:

import java.util.Scanner;
public class Test{
           public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.println("Enter any Sentence:");
      String ss = s.nextLine( );
      String[ ] a = ss.split(" ");
      String res="";
      for(int i=0;i<a.length;i++){
         StringBuffer s1 = new StringBuffer(a[i]);
         s1.reverse( );
         res+=s1+" ";
      }
      System.out.println(res);
    }
}


Source Code: download


Sunday, 3 April 2016

Write a program which accepts 2 strings from the user and performs string concatenation in JAVA



Write a program which accepts 2 strings from the user and performs the following operation on it.

Example1:

String1 = "JAVA"
String2 = "PYTHON"
String3 must be "JAVANOHTYP"

Example2:

String1 = "Vignesh"
String2 = "Sundeep"
String3 must be "VigneshpeednuS"

Both strings must be concatenated but after reversing the second string (as shown in string3 above).

Program:

import java.util.Scanner;

public class String_Program {
public static void main(String[ ] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any two strings: ");
String s1 = s.next();
String s2 = s.next();
StringBuffer sb = new StringBuffer(s2);
sb.reverse( );
System.out.println(s1+sb);
}
}

Input/Output:

Enter any two strings:
Raju
Ramana
RajuanamaR


String Manipulation Program-1 in JAVA


Description:

10 digit number: 1234567812
10 digit number in words: ONETWOTHREEFOURFIVESIXSEVENEIGHTONETWO

Constraints:

1) remove duplicate letters in the above string.
2) final string must be in sorted order.

Final String: EFGHINORSTUVWX

Program:

import java.util.Arrays;
import java.util.Scanner;

public class String_Program{
public static boolean ISin(String str, char c) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter 10 digit number: ");
long n = s.nextLong();
String str = n + "";
String res = "";
String no[] = new String[] { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" };
if (str.length( ) < 10 || str.length( ) > 10) {
System.out.println("Invalid Input");
} else {
for (int i = 0; i < str.length(); i++) {
res = res + no[Integer.parseInt(str.charAt(i) + "")];
}
}
String temp = "";
for (int i = 0; i < res.length(); i++) {
if (!ISin(temp, res.charAt(i))) {
temp = temp + res.charAt(i);
}
}
String r[] = new String[temp.length()];
for (int i = 0; i < r.length; i++) {
r[i] = temp.charAt(i) + "";
}
String rr = "";
Arrays.sort(r);
for (int i = 0; i < r.length; i++) {
rr += r[i];
}
System.out.println(rr);

}
}

Input/Output:

Enter 10 digit number:
1234567812
EFGHINORSTUVWX



Sum of last digits of two given numbers program in JAVA



Program:

import java.util.Scanner;
public class Sum_of_last_digits_of_two_given_numbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any two numbers: ");
int a = s.nextInt();
int b = s.nextInt();
String s1 = a+""; //converting integer to string
String s2 = b+""; //converting integer to string
int c = Integer.parseInt(s1.charAt(s1.length()-1)+"") + Integer.parseInt(s2.charAt(s2.length()-1)+"");
System.out.println("Sum of last digits of two given numbers is: "+c);
}
}

Input/Output:

Enter any two numbers:
123
145
Sum of last digits of two given numbers is: 8



Sunday, 1 November 2015

Java Program to print greatest of two numbers


Program:

import java.io.*;
import java.util.*;
class greater
{
public static void main(String[ ] args) {
System.out.println("Enter two values: ");
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
if(a>b)
System.out.println(a + " is Greater than " + b);
else
System.out.println(b + " is Greater than " + a);
}
}

Input/Output:

Enter two values:
5
6
6 is Greater than 5

Thursday, 6 August 2015

Java program for multiplication of original matrix and transpose matrix


Input: s = 1
Matrix elements increased by one.
Original Matrix:

1  2  3 
4  5  6
7  8  9

Transpose Matrix:

1  4  7
2  5  8
3  6  9

Multiplication of two matrices:

14  32  50
32  77  122
50  122  194

Program:

import java.io.*;
import java.util.*;
public class matmul
{
          public static void main( String[ ] args ) {
          Scanner n = new Scanner( System.in );
          int s, i, j, k;
          int a[ ][ ] = new int[ 10 ] [ 10 ];
          int b[ ][ ] = new int[ 10 ][ 10 ];
          int mul[ ][ ] = new int[ 10 ][ 10 ];
          System.out.println( "Enter S value: " );
          s = n.nextInt( );
          for( i = 0 ; i < 3 ; i++)
         {
                 for( j = 0 ; j < 3 ; j++ )
                {
                        a[ i ][ j ] = s;
                        s = s + 1;
                 }
          }
          System.out.println( "Original Matrix: " );
          for( i = 0 ; i < 3 ; i++ )
          {
                  for( j = 0 ; j < 3 ; j++ )
                 {
                        System.out.print( a[ i ][ j ] + "  " );
                  }
                  System.out.println("");
           }
           for( i = 0 ; i < 3 ; i++ )
          {
                   for( j = 0 ; j < 3 ; j++ )
                  {
                         b[ i ][ j ] = a[ j ][ i ];
                   }
           }
           System.out.println( "Traspose Matrix:" );
           for( i = 0 ; i < 3 ; i++)
           {
                   for( j = 0 ; j < 3 ; j++)
                  {
                          System.out.print( b[ i ][ j ] + "  " );
                   }
                   System.out.println( "" );
            }
            for(i=0 ; i < 3 ; i++ )
           {
                    for( j = 0 ; j < 3 ; j++)
                   {
                           for( k = 0 ; k < 3 ; k++ )
                          {
                                  mul[ i ] [ j ] = mul[ i ] [ j ] + ( a[ i ] [ k ] * b[ k ] [ j ] );
                           }
                    }
             }
             System.out.println( "Multiplication of two matrices: " );
             for( i = 0 ; i < 3 ; i++ )
            {
                    for( j = 0 ; j < 3 ; j++ )
                   {
                            System.out.print( mul[i][j] + "  " );
                    }
                    System.out.println( " " );
             }
       }
}



Sunday, 15 February 2015

What is the output of the following Java Program-1



Program:

import java.io.*;
public class test
{
           public static void main(String args[])
           {
                      System.out.println(7|8);
           }
}

Options:

a) 7
b) 8
c) 15
d) error

Explanation:

Option: c) is correct.

Here we can do the OR( | ) operation for 7 and 8.

The Binary value of 7 is  0111
The Binary value of 8 is 1000

      7 --->  0111
      8 --->  1000
----------------------
                 1111 
----------------------

The Decimal value of 1111 is 15.

So answer is 15.




Thanks for visiting.......

Wednesday, 24 December 2014

Write a Java Program to Print the Maximum and Minimum Numbers in the array

Input: 

    3,5,2,8,1,10

Output:

     Min Number: 1
     Max Number: 10


-->We will give only single input of array of Numbers Separated by Coma.

Program:

import java.io.*;
import java.util.*;
import java.util.StringTokenizer;

public class arrtok
{
    public static void main(String args[])
    {
        String n,temp;
        int i=0,j=0,p=0,temp1;
        int a[]=new int[20];
        Scanner s=new Scanner(System.in);
        n=s.next();
        StringTokenizer st = new StringTokenizer(n,",");
        while(st.hasMoreTokens())
        {
            temp=st.nextToken();
            a[i]=Integer.parseInt(temp);
            i++;
            j++;
      }
      System.out.println("Elements in Array are");
      for(i=0;i<j;i++)
      {
      System.out.println(a[i]);
      }
      for(i=0;i<j;i++)
      {
      p=i;
      while(p>0&&(a[p]<a[p-1]))
      {
      temp1=a[p];
      a[p]=a[p-1];
      a[p-1]=temp1;
      p--;
      }
      }
      System.out.println("Min Number: "+a[0]);
      System.out.println("Min Number: "+a[j-1]);

    }
}

See more...

Java Program to find largest number in an array
Java Program to find Minimum and Maximum Numbers in an Array
Find Largest and Smallest Number in an array
Finding minimum and maximum values in an array
Java Minimum and Maximum values in Array
Java Program to Find the largest and smallest number in n numbers
Java Program to display Minimum and Maximum Numbers in an Array
Java Program to print Minimum and Maximum Numbers in an Array
How to find Minimum and Maximum Numbers in an Array