Saturday, 7 January 2017

Top 25 Java Logical Programs

To 25 Java logical program . Just go through all of them if you are going to appear in a jva written test or interview.

1) Write a program to swap two numbers without using temporary variable.
2) Write a program which takes 1 to 100 number , print "A" if number is divisible by 3 , print "B" if number is divisible by 5 and print "C" if it is divisivle by 3 and 5 both else print "D".
3) Write a program to check the given number is a prime number or not.
4) Write a program to check if number is perfect square or not.
5) Write a program to calculate factorial of a number.
6) Write a program to calculate power of a number.
7) Write a program to reverse a number.
8) Write a java program which will reverse the string.
9) Write a program in java to check if string is palindrome.
10) Write a program to find top two maximum numbers in a array.
11) Write a program to find common elements between two arrays.
12) Write a program to print fibonacci series.
13) Write a program which can extract duplicate element from an array.
14) Write a program to find sum of each digit in the given number using recursion.
15) Write a program to check the given number is binary number or not.
16) Write a program to convert binary to decimal number.
17) Write a program to convert decimal number to binary.
18) Wrie a program to find out duplicate characters in a string.
19) Write a program to create a following pyramid.
20) Write a program to create a following pyramid.
21) Write a program to create a following pyramid.
22) Write a program to draw a following pyramid.
23) Write a program to draw a following pyramid.
24) Write a program for Bubble Sort in java.
25) Write a program for Insertion Sort in java.


1) Write a program to swap two numbers without using temporary variable. 

  class A
  {
      public static void main(String[] args) 
      {
          int i=3 ,j=5;
          System.out.println("value if i & j before : i="+i+" and j= "+j);
          i= i+j;
          j = i - j;
          i = i - j;
          System.out.println("value if i & j After : i="+i+" and j= "+j);
      }
  }
Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java A
value if i & j before : i=3 and j= 5
value if i & j After : i=5 and j= 3

2) Write a program which takes 1 to 100 number , print "A" if number is divisible by 3 , print "B" if number is divisible by 5 and print "C" if it is divisible by 3 and 5 both else print "D". 

class Demo
{
 
      public static void main(String args[]) 
      {
          for( int i=1; i<=100; i++ ) 
          {
              if ( i%3 == 0 && i%5 ==0)
              System.out.println(i+"="+"C");
              else if( i%3 == 0 )
              System.out.println(i+"="+"A");
              else if( i%5 == 0 )
              System.out.println(i+"="+"B");
              else
              System.out.println(i+"="+"D");
 
      }
 
      }
 
  }


3) Write a program to check the given number is a prime number or not. 

  import java.util.*;
 
  public class Demo 
 
  {
      public boolean isPrimeNumber(int number)
      {         
          for(int i=2; i<number; i++)//for(int i=2; i<=number/2; i++)
          {
              if(number % i == 0)
              {
                  return false;
              }
          }
          return true;
      }     
      public static void main(String a[])
      {
          int n;
          System.out.println("Enter a number to check prime");
          Scanner in=new Scanner(System.in);
          n=in.nextInt();
          Demo mpc = new Demo();
          System.out.println("Is "+ n + " prime number? "+mpc.isPrimeNumber(n));
      }
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo
Enter a number to check prime
13
Is 13 prime number? true
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo
Enter a number to check prime
18
Is 18 prime number? false

4) Write a program to check if number is perfect square or not.
  import java.util.Scanner;
 
  class Demo
  {
      public static void main(String args[])
      {
          int n;
          boolean isSquare = false;
          System.out.println("Enter the number to check perfect square");
          Scanner in=new Scanner(System.in);
          n = in.nextInt();
          
          for(int i=0; i<=n; i++)
          {
              if(n == i*i)
              {
                  isSquare = true;
                  break;
              }
          }
          if(isSquare)
          {
              System.out.println("perfect square");
          }    
          else
          {
              System.out.println("Not a perfect square");
          }
      }
  }
Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo
Enter the number to check perfect square
3
Not a perfect square
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo
Enter the number to check perfect square
49
perfect square

5) Write a program to calculate factorial of a number. 

  
import java.util.Scanner;
  class Factorial
  {
     public static void main(String args[])
     {
        int num,fact = 1;
   
        System.out.println("Enter an integer to calculate it's factorial");
        Scanner in = new Scanner(System.in);
   
        num = in.nextInt();
   
        if ( num < 0 )
           System.out.println("Number should be non-negative.");
        else
        {
           for (int i = 1 ; i <= num ; i++ )
              fact = fact*i;
   
           System.out.println("Factorial of "+num+" is = "+fact);
        }
     }
  }

Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Factorial 
Enter an integer to calculate it's factorial
5
Factorial of 5 is = 120

6) Write a program to calculate power of a number. 

  
import java.util.Scanner;
   
  class Demo
  {
     public static void main(String args[])
     {
        int num,pow,result = 1;
        System.out.println("Enter number on which power required");
        Scanner n = new Scanner(System.in);
        num = n.nextInt();
        System.out.println("Enter power");
        Scanner p = new Scanner(System.in);
        pow = p.nextInt();
        for(int i=1; i<=pow; i++ )
        {
              result = result * num ;
        }
        System.out.println("The result is :"+result);
      }
  }

Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo 
Enter number on which power required
3
Enter power
3
The result is :27

7) Write a program to reverse a number. 

import java.util.Scanner;
   
  class ReverseNumber
  {
     public static void main(String args[])
     {
        int n, reverse = 0;
   
        System.out.println("Enter the number to reverse");
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
   
        while( n != 0 )
        {
            reverse = reverse * 10;
            reverse = reverse + n%10;
            n = n/10;
        }
   
        System.out.println("Reverse of entered number is "+reverse);
     }
  }
  
Output:
Enter the number to reverse
324
Reverse of entered number is 423

8) Write a java program which will reverse the string. 

  
import java.util.Scanner;

class ReverseString
  {
      public static void main(String args[])
      {
          String original,reverse="";
          Scanner in = new Scanner(System.in);
          System.out.println("Enter a string");
          original = in.nextLine();
          int len = original.length();
          for( int i = len -1 ; i >=0 ; i-- )
              reverse = reverse + original.charAt(i);

          System.out.println("Reverse of entered string is: "+reverse);
     }
  }


Output:
Enter a string
i love java
Reverse of entered string is: avaj evol i

9) Write a program in java to check if string is palindrome. 

import java.util.Scanner;
   
  class palindrome
  {
     public static void main(String args[])
     {
        String original, reverse = "";
        Scanner in = new Scanner(System.in);
   
        System.out.println("Enter a string to reverse");
        original = in.nextLine();
   
        int length = original.length();
   
        for ( int i = length - 1 ; i >= 0 ; i-- )
           reverse = reverse + original.charAt(i);
              
              if(reverse.equalsIgnoreCase(original))
              {
                  System.out.println("The string is Palindrome");
              }
              else
              {
                  System.out.println("Not Palindrome");
              }
     }
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java palindrome 
Enter a string to reverse
mukesh
Not Palindrome
mukesh@ubuntu:~/Desktop/javaPrograms$ java palindrome 
Enter a string to reverse
aka
The string is Palindrome

10) Write a program to find top two maximum numbers in a array. 

  
public class Demo
 
  {
 
      public static void main(String[] args)
 
      {
 
          int maxNum = 0;
 
          int secondMaxNum = 0;
 
          int[] anArray; 
 
          anArray =new int [10];
 
          anArray[0] = 100; 
 
          anArray[1] = 200; 
 
          anArray[2] = 300; 
 
          anArray[3] = 400;
 
          anArray[4] = 500;
 
          if(anArray.length == 0)
 
          {
 
              System.err.println("anArray is empty");
 
              return;
 
          }
 
          for(int i=0; i < anArray.length; i++)
 
          {
 
              int currNum = anArray[i];
 
              if(maxNum < currNum)
 
              {
 
                  secondMaxNum = maxNum;
 
                  maxNum = currNum;
 
              }
 
              else if(secondMaxNum < currNum)
 
              {
 
                  secondMaxNum = currNum;
 
              }
 
          }
 
              System.out.println("Max. number is "+maxNum);
 
              System.out.println("Second Max. is "+secondMaxNum);
 
      }
 
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo 
Max. number is 500
Second Max. is 400

11) Write a program to find common elements between two arrays. 

  public class CommonElementsInArray 
 
  {
 
      public static void main(String a[])
 
      {
 
          int[] arr1 = {4,7,3,9,2};
 
          int[] arr2 = {3,2,12,9,40,32,4};
 
          for(int i=0;i<arr1.length;i++)
 
          {
 
              for(int j=0;j<arr2.length;j++)
 
              {
 
                  if(arr1[i]==arr2[j])
 
                  {
 
                      System.out.println(arr1[i]);
 
                  }
 
              }
 
          }
 
      }
 
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java CommonElementsInArray 
4
3
9
2

12) Write a program to print fibonacci series. 

  /*Write a program to find Fibonacci series of a given no. Example : 
 
  Input - 8 Output - 1 1 2 3 5 8 13 21 */ 
 
  class Fibonacci
 
  { 
 
      public static void main(String args[])
 
      { 
 
          int num = Integer.parseInt(args[0]);
 
           //taking no. as command line argument. System.out.println("*****Fibonacci Series*****"); 
 
          int f1, f2=0, f3=1; 
 
          for(int i=1;i<=num;i++)
 
          { 
 
              System.out.print(" "+f3+" "); 
 
              f1 = f2; 
 
              f2 = f3; 
 
              f3 = f1 + f2; 
 
          } 
 
      } 
 
  } 


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Fibonacci 6
 1  1  2  3  5  8
 
13) Write a program which can extract duplicate element from an array. 

import java.util.Scanner;


class Demo
 
  {
 
      public static void main(String[] args)
 
      {
 
          Scanner sc=new Scanner(System.in);
 
          System.out.println("enter the size of array you want");
 
          int num=sc.nextInt();
 
          int array[]=new int[num];
 
          System.out.println("enter the" + num + "elements");
 
          for(int i=0; i < num; i++)
 
          {
 
              array[i]=sc.nextInt();
 
          }
 
          printRepeating(array, array.length);
 
      }
 
      static void printRepeating(int arr[], int size)
 
      {
 
            int i, j;
 
            System.out.println(" Repeating elements are ");
 
            for(i = 0; i < size; i++)
 
          for(j = i+1; j < size; j++)
 
            if(arr[i] == arr[j])
 
          System.out.println( arr[i]);
 
      } 
 
  }


Output:
enter the size of array you want
5
enter the5elements
2
3
2
4
3
 Repeating elements are 
2
3

14) Write a program to find sum of each digit in the given number using recursion. 

  public class MyNumberSumRec {
 
   
 
      int sum = 0;
 
       
 
      public int getNumberSum(int number){
 
           
 
          if(number == 0){
 
              return sum;
 
          } else {
 
              sum += (number%10);
 
              getNumberSum(number/10);
 
          }
 
          return sum;
 
      }
 
       
 
      public static void main(String a[]){
 
          MyNumberSumRec mns = new MyNumberSumRec();
 
          System.out.println("Sum is: "+mns.getNumberSum(223));
 
      }
 
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java MyNumberSumRec 
Sum is: 7

15) Write a program to check the given number is binary number or not. 

  import java.util.*;
 
  public class Demo 
 
  {    
 
      public static void main(String a[])
 
      {
 
          boolean notBinary=false;
 
          System.out.println("Enter the number");
 
          Scanner in=new Scanner(System.in);
 
          int num=in.nextInt();
 
          while( num != 0 )
 
          {
 
              int rem=num % 10;
 
              
 
              if(rem >1)
 
              {
 
                  notBinary=true;
 
                  break;
 
              }
 
              num=num/10;
 
          }
 
          if(notBinary)
 
          {
 
              System.out.println("not binary");
 
          }
 
          else
 
          {
 
              System.out.println("is binary");
 
          }
 
              
 
      }
 
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo 
Enter the number
1001
is binary
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo 
Enter the number
1003
not binary

16) Write a program to convert binary to decimal number. 

  public class BinaryToDecimal 
 
  { 
 
      public int getDecimalFromBinary(int binary)
 
      {         
 
          int decimal = 0;
 
          int power = 0;
 
          while(true)
 
          {
 
              if(binary == 0)
 
              {
 
                  break;
 
              } 
 
              else 
 
              {
 
                  int tmp = binary%10;
 
                  decimal += tmp*Math.pow(2, power);
 
                  binary = binary/10;
 
                  power++;
 
              }
 
          }
 
          return decimal;
 
      }     
 
      public static void main(String a[]){
 
          BinaryToDecimal bd = new BinaryToDecimal();
 
          System.out.println("11 ===> "+bd.getDecimalFromBinary(11));
 
          System.out.println("110 ===> "+bd.getDecimalFromBinary(110));
 
          System.out.println("100110 ===> "+bd.getDecimalFromBinary(100110));
 
      }
 
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java BinaryToDecimal 
11 ===> 3
110 ===> 6
100110 ===> 38
        
        
17) Write a program to convert decimal number to binary. 

  import java.util.Scanner;
 
  class Demo 
 
  {
 
      public static void main(String args[])
 
      {
 
          int binary[] = new int[25];
 
          int index = 0;
 
          Scanner sc=new Scanner(System.in);
 
          System.out.print("Enter the decimal number");
 
          int number=sc.nextInt();
 
          while(number > 0)
 
          {
 
              binary[index++] = number%2;
 
              number = number/2;
 
          }
 
          for(int i = index-1;i >= 0;i--)
 
          {
 
              System.out.print(binary[i]);
 
          }
 
      }     
 
  }


Output:
Enter the decimal number4
100  

18) Write a program to find out duplicate characters in a string. 

  import java.util.Scanner;  
 
    
 
  public class Same_Occurence_String 
 
  {  
 
      public static void main(String[] args) 
 
      {  
 
          Scanner scan=new Scanner(System.in);  
 
          System.out.println("enter the string");  
 
          String g=scan.next();  
 
          System.out.println("enter the character you want to search for");  
 
            String l=  scan.next();  
 
           char[] c= l.toCharArray();  
 
             int  count=0;  
 
             for(int i=0;i<=g.length()-1;i++)
 
          {  
 
             for(int k=0;k<=c.length-1;k++)  
 
             {  
 
                 if(g.charAt(i)==c[k])
 
                 {  
 
                         System.out.println("indexes where duplicate character are"+i);  
 
                         count++;    
 
                     }  
 
                 }  
 
          }  
 
             System.out.println("no. of duplicate character"+count);    
 
      }  
 
  } 


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Same_Occurence_String 
enter the string
coolstuffsite
enter the character you want to search for
s
indexes where duplicate character are4
indexes where duplicate character are9
no. of duplicate character2

19) Write a program to create a following pyramid.
*
**
***
****
*****
******
*******
import java.util.Scanner;
 
  public class Demo
 
  {
 
         public static void main(String args[])
 
      {    
 
          Scanner sc=new Scanner(System.in);
 
          System.out.println("enter number");
 
          int num=sc.nextInt();
 
          for(int i=0; i<=num; i++)
 
          {
 
              for(int j=0; j<=i; j++)
 
              System.out.print("*");
 
              System.out.println("");
 
          }
 
      }
 
  }


20) Write a program to create a following pyramid.
********
*******
******
*****
****
***
**
*
import java.util.Scanner;
 
  public class Demo
 
  {
 
         public static void main(String args[])
 
      {    
 
          Scanner sc=new Scanner(System.in);
 
          System.out.println("enter number");
 
          int num=sc.nextInt();
 
          for(int i=num; i>=0; i--)
 
          {
 
              for(int j=0; j<=i; j++)
 
              System.out.print("*");
 
              System.out.println("");
 
          }
 
      }
 
  }


21) Write a program to create a following pyramid.
      *
     ***
    *****
   *******
  *********
 ***********
import java.util.Scanner;
 
  class Demo
 
  {
 
      public static void main(String args[])
 
      {
 
          Scanner sc=new Scanner(System.in);
 
          System.out.println("enter number");
 
          int num=sc.nextInt();
 
          for (int i=0; i<num; i++)
 
            {
 
               for (int k=0; k<num-i; k++)
 
               {
 
                  System.out.print(" ");
 
               }
 
               for (int j=0; j<i*2+1; j++)
 
              {
 
                  System.out.print("*");
 
              }
 
               System.out.println("");
 
            }
 
      }
 
  } 


22) Write a program to draw a following pyramid.
     *
    * *
   * * *
  * * * *
 * * * * *
 
  import java.util.Scanner; (flipkart)
 
  class Demo
 
  {
 
      public static void main(String args[])
 
      {
 
          Scanner sc=new Scanner(System.in);
 
          System.out.println("enter number");
 
          int num=sc.nextInt();
 
          for (int i=0; i<num; i++)
 
            {
 
               for (int j=0; j<num-i; j++)
 
               {
 
                  System.out.print(" ");
 
               }
 
              boolean flag=true;
 
               for (int k=0; k<i*2+1; k++)
 
              {
 
                  
 
                  if(flag)
 
                  {
 
                      System.out.print("*");
 
                      flag=false;
 
                  }
 
                  else
 
                  {
 
                      System.out.print(" ");
 
                      flag=true;
 
                  }
 
              }
 
               System.out.println("");
 
            }
 
      }
 
  }
 
  
    
23) Write a program to draw a following pyramid.    
   #&
  ##&&
 ###&&&
####&&&&
$$$$****
 $$$***
  $$**
   $*
    
  public class Demo 
 
  {
 
      public static void main(String args[])
 
      {
 
      for (int i = 1; i <= 5; i++) 
 
      {
 
           for (int s = 5; s > i; s--) 
 
          {
 
              System.out.print(" ");
 
          }
 
          for (int j = 1; j < i; j++) 
 
          {
 
              System.out.print("#");
 
          }
 
          for (int j = 1; j < i; j++) 
 
          {
 
              System.out.print("&");
 
          }
 
              System.out.println("");
 
          }
 
          for (int i = 1; i <= 5; i++) 
 
          {
 
              for (int s = 1; s < i; s++) 
 
              {
 
                  System.out.print(" ");
 
              }
 
              for (int j = 5; j > i; j--) 
 
              {
 
                  System.out.print("$");
 
              }
 
              for (int j = 5; j > i; j--) 
 
              {
 
                  System.out.print("*");
 
              }
 
              System.out.println("");
 
          }
 
      }
 
  }


24) Write a program for Bubble Sort in java. 

  public class MyBubbleSort 
 
  {
 
      // logic to sort the elements
 
      public static void bubble_srt(int array[]) 
 
      {
 
          int n = array.length;
 
          int k;
 
          for (int m = n; m >= 0; m--) 
 
          {
 
              for (int i = 0; i < n - 1; i++) 
 
              {
 
                  k = i + 1;
 
                  if (array[i] > array[k]) 
 
                  {
 
                      swapNumbers(i, k, array);
 
                  }
 
              }
 
              printNumbers(array);
 
          }
 
      }
 
      private static void swapNumbers(int i, int j, int[] array) 
 
      {
 
          int temp;
 
          temp = array[i];
 
          array[i] = array[j];
 
          array[j] = temp;
 
      }
 
      private static void printNumbers(int[] input) 
 
      {         
 
          for (int i = 0; i < input.length; i++) 
 
          {
 
              System.out.print(input[i] + ", ");
 
          }
 
          System.out.println("\n");
 
      }
 
      public static void main(String[] args) 
 
      {
 
          int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
 
          bubble_srt(input);
 
      }
 
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java MyBubbleSort 
2, 4, 6, 9, 12, 23, 0, 1, 34, 
2, 4, 6, 9, 12, 0, 1, 23, 34, 
2, 4, 6, 9, 0, 1, 12, 23, 34, 
2, 4, 6, 0, 1, 9, 12, 23, 34, 
2, 4, 0, 1, 6, 9, 12, 23, 34, 
2, 0, 1, 4, 6, 9, 12, 23, 34, 
0, 1, 2, 4, 6, 9, 12, 23, 34, 
0, 1, 2, 4, 6, 9, 12, 23, 34, 
0, 1, 2, 4, 6, 9, 12, 23, 34, 
0, 1, 2, 4, 6, 9, 12, 23, 34,

25) Write a program for Insertion Sort in java. 

  public class MyInsertionSort 
 
  { 
 
      public static void main(String[] args) 
 
      {
 
           
 
          int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
 
          insertionSort(input);
 
      }  
 
      private static void printNumbers(int[] input) 
 
      {
 
           
 
          for (int i = 0; i < input.length; i++) 
 
          {
 
              System.out.print(input[i] + ", ");
 
          }
 
          System.out.println("\n");
 
      }
 
      public static void insertionSort(int array[]) 
 
      {
 
          int n = array.length;
 
          for (int j = 1; j < n; j++) 
 
          {
 
              int key = array[j];
 
              int i = j-1;
 
              while ( (i > -1) && ( array [i] > key ) ) 
 
              {
 
                  array [i+1] = array [i];
 
                  i--;
 
              }
 
              array[i+1] = key;
 
              printNumbers(array);
 
          }
 
      }
 
  }


Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java MyInsertionSort 
2, 4, 9, 6, 23, 12, 34, 0, 1, 
2, 4, 9, 6, 23, 12, 34, 0, 1, 
2, 4, 6, 9, 23, 12, 34, 0, 1, 
2, 4, 6, 9, 23, 12, 34, 0, 1, 
2, 4, 6, 9, 12, 23, 34, 0, 1, 
2, 4, 6, 9, 12, 23, 34, 0, 1, 
0, 2, 4, 6, 9, 12, 23, 34, 1, 
0, 1, 2, 4, 6, 9, 12, 23, 34, 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...