Wednesday, October 12, 2016

Binary search in JAVA programming

  1. import java.util.Scanner;
  2. public class BinarySearch {
  3.  
  4.     public static void main(String[] args) {
  5.  
  6.         Scanner myScanner=new Scanner(System.in);
  7.  
  8.         System.out.print("Enter the array index : ");
  9.  
  10.         int n=myScanner.nextInt();
  11.  
  12.         int[] arr=new int[n];
  13.  
  14.         System.out.print("Enter "+n+" numbers : ");
  15.  
  16.         for (int i = 0; i <n; i++) {
  17.  
  18.             arr[i]=myScanner.nextInt();
  19.  
  20.         }
  21.  
  22.         System.out.print("Enter search number : ");
  23.  
  24.         int s=myScanner.nextInt();
  25.  
  26.         int f,l,m;
  27.  
  28.         f=0;
  29.  
  30.         l=n-1;
  31.  
  32.         m=(f+l)/2;
  33.  
  34.         while(f<=l)
  35.  
  36.         {
  37.  
  38.             if(arr[m]==s){
  39.  
  40.                 System.out.print("The number is found in the array & Location is : "+(m+1));
  41.  
  42.                 break;
  43.  
  44.             }
  45.  
  46.             else if(arr[m]<s){
  47.  
  48.                 f=m+1;
  49.  
  50.             }
  51.  
  52.             else{
  53.  
  54.                 l=m-1;
  55.  
  56.             }
  57.  
  58.             m=(f+l)/2;
  59.  
  60.         }
  61.  
  62.         if(f>l){
  63.             System.out.println("The number is not found in the array");
  64.  
  65.         }      
  66.     }
  67.  
  68. }
  69.  

Tuesday, October 11, 2016

String palindrome in JAVA programming

  1. import java.util.Scanner;
  2.  
  3. public class StringPalin {
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner myScanner=new Scanner(System.in);
  9.  
  10.         System.out.print("Enter the string : ");
  11.  
  12.         String str1=myScanner.nextLine();
  13.  
  14.         String rev="";
  15.  
  16.  
  17.  
  18.         for (int i = str1.length()-1; i>=0; i--) {
  19.  
  20.             rev=rev+str1.charAt(i);
  21.  
  22.         }
  23.  
  24.  
  25.  
  26.         if(str1.compareToIgnoreCase(rev)==0){  ////you can also use str1.equals(rev)
  27.  
  28.             System.out.println(str1+" is Palindrome");
  29.  
  30.         }else {
  31.  
  32.             System.out.println(str1+" is Not palindrome");
  33.  
  34.         }
  35.  
  36.     }
  37.  
  38.  
  39. }
  40.  
  41. //Enter the string : Madam
  42.  
  43. //Madam is Palindrome

String swapping in JAVA programming

  1. import java.util.Scanner;
  2.  
  3. public class StringSwapping {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner myScanner=new Scanner(System.in);      
  8.  
  9.         String a,b,temp;
  10.  
  11.         a="Alamgir";
  12.  
  13.         b="Hossain";
  14.  
  15.         System.out.println("Before swapping : A = "+a+" and B = "+b);
  16.  
  17.         temp=a;
  18.  
  19.         a=b;
  20.  
  21.         b=temp;       
  22.  
  23.         System.out.println("After swapping : A  = "+a+" and B = "+b);
  24.  
  25.     }
  26.  
  27. }
  28.  
  29. //Before swapping : A = Alamgir and B = Hossain
  30.  
  31. //After swapping : A  = Hossain and B = Alamgir

Factorial using recursion in JAVA programming

  1. import java.util.Scanner;
  2.  
  3. public class FactorialRecursion {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner myScanner=new Scanner(System.in);
  8.  
  9.         System.out.print("Enter a number : ");
  10.  
  11.         int n=myScanner.nextInt();      
  12.  
  13.         System.out.print("Factorial of "+n+" is : "+factorial(n));      
  14.  
  15.     }
  16.  
  17.     static int factorial(int n)
  18.  
  19.     {
  20.  
  21.         int f;
  22.  
  23.         if(n==0){
  24.  
  25.             f=1;
  26.  
  27.         }else{
  28.  
  29.             f=n*factorial(n-1);
  30.  
  31.         }
  32.  
  33.         return f;
  34.  
  35.     }
  36.  
  37. }
  38.  
  39. //Enter a number : 5
  40.  
  41. //Factorial of 5 is : 120

Fibonacci series using recursion in JAVA programming

  1. import java.util.Scanner;
  2.  
  3. public class FibonacciRecursion {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner myScanner=new Scanner(System.in);
  8.  
  9.         //---------Input-------------
  10.  
  11.         System.out.print("Enter how many terms you want : ");
  12.  
  13.         int n=myScanner.nextInt();      
  14.  
  15.         for (int i = 1; i <=n; i++) {
  16.  
  17.             System.out.print(fib(i)+"\t");
  18.  
  19.         }
  20.  
  21.     }  
  22.  
  23.     //--------------Fibonacci method----------------
  24.  
  25.     static int fib(int n)
  26.  
  27.     {
  28.  
  29.         int f;
  30.  
  31.         if (n==1) {
  32.  
  33.             f=0;
  34.  
  35.         }else if(n==2){
  36.  
  37.             f=1;
  38.  
  39.         }
  40.  
  41.         else{
  42.  
  43.             f=fib(n-1)+fib(n-2);
  44.  
  45.         }
  46.  
  47.         return f;
  48.  
  49.     }
  50.  
  51. }
  52.  
  53. //Enter how many terms you want : 10
  54.  
  55. //0    1    1    2    3    5    8    13    21    34   
  56.  
  57.  
  58.  

Monday, October 10, 2016

Selection sort in JAVA programming

  1. package SolveProblem;//Package name---------
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SelectionSort {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner myScanner=new Scanner(System.in);
  10.  
  11.         //-----------------input----------------
  12.  
  13.         System.out.print("Enter how many number : ");
  14.  
  15.         int n=myScanner.nextInt();       
  16.  
  17.         int[] arr=new int[n];
  18.  
  19.         int i;
  20.  
  21.         System.out.print("Enter "+n+" numbers : ");
  22.  
  23.         for (= 0; i < n; i++) {
  24.  
  25.             arr[i]=myScanner.nextInt();
  26.  
  27.         }
  28.  
  29.         int pos,temp,j,k;       
  30.  
  31.         // -----------sort  start-------------
  32.  
  33.         for (= 1; k < n; k++) {
  34.  
  35.             pos=k-1;
  36.  
  37.             for (= k; j < n; j++) {
  38.  
  39.                 if (arr[pos]>arr[j]) {
  40.  
  41.                     pos=j;   
  42.  
  43.                 }
  44.  
  45.             }
  46.  
  47.             //-----------when position in not equal with k-1 then interchange--------------
  48.  
  49.             if(pos!=k-1){
  50.  
  51.                 temp=arr[k-1];
  52.  
  53.                 arr[k-1]=arr[pos];
  54.  
  55.                 arr[pos]=temp;
  56.  
  57.             }
  58.  
  59.         }
  60.  
  61.         // ----------------------output------------------
  62.  
  63.         System.out.print("Sorted list is : ");
  64.  
  65.         for (= 0; i <n; i++) {
  66.  
  67.             System.out.print(arr[i]+"\t");
  68.  
  69.         }
  70.  
  71.         System.out.println();
  72.  
  73.     }
  74.  
  75.  
  76. }

Round the value of Given number in Java

  1. import java.util.Scanner;
  2.  
  3. public class abc {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner myScanner=new Scanner(System.in);
  8.         float num;
  9.         System.out.print("Enter the number : ");
  10.         num=myScanner.nextFloat();
  11.         float ans=Math.round(num);
  12.  
  13.         System.out.println("The digit count is : "+ans);
  14.  
  15.     }
  16. }
  17.  
  18. //Enter the number : 11.50
  19.  
  20. //The digit count is : 12.0

Power of Given number in Java

  1. import java.util.Scanner;
  2.  
  3. public class Power {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner myScanner=new Scanner(System.in);
  8.  
  9.         System.out.print("Enter the number : ");
  10.  
  11.         int num=myScanner.nextInt();
  12.  
  13.         System.out.print("Enter power of : ");
  14.  
  15.         int power=myScanner.nextInt();      
  16.  
  17.         long sum;
  18.  
  19.         sum=(long) Math.pow(num, power);      
  20.  
  21.         System.out.println(num+" power "+power+" is : "+sum);
  22.  
  23.     }
  24.  
  25. }
  26.  
  27. //Enter the number : 25
  28.  
  29. //Enter power of : 5
  30.  
  31. //25 power 5 is : 9765625

Sum of digits of the given number in Java programming

  1. import java.util.Scanner;
  2.  
  3. public class SumDigits {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner myScanner=new Scanner(System.in);       
  8.  
  9.         int n;
  10.  
  11.         System.out.print("Enter the number : ");
  12.  
  13.         n=myScanner.nextInt();
  14.  
  15.         int rem,sum=0;
  16.  
  17.         while (n!=0) {
  18.  
  19.             rem=n%10;
  20.  
  21.             sum=sum+rem;
  22.  
  23.             n=n/10;
  24.  
  25.         }       
  26.  
  27.         System.out.println("Sum of digits of the given number is : "+sum);
  28.  
  29.     }
  30.  
  31. }
  32.  
  33. //Enter the number : 123456
  34.  
  35. //Sum of digits of the given number is : 21

Armstrong number between a to b in JAVA programming

  1. import java.util.Scanner;
  2.  
  3. public class ArmstrongBTNum {
  4.  
  5.     static int armstrong(int n)
  6.  
  7.     {
  8.  
  9.         int sum=0,rem,num,ck=0;
  10.  
  11.         num=n;
  12.  
  13.         while(num!=0)
  14.  
  15.         {
  16.  
  17.             rem=num%10;
  18.  
  19.             sum=sum+(rem*rem*rem);
  20.  
  21.             num=num/10;
  22.  
  23.         }
  24.  
  25.         return sum;
  26.  
  27.     }
  28.  
  29.  
  30.  
  31.     public static void main(String[] args) {
  32.  
  33.         Scanner myScanner=new Scanner(System.in);
  34.  
  35.         int a,b,i,ck;
  36.  
  37.         System.out.print("Enter two numbers : ");
  38.  
  39.         a=myScanner.nextInt();
  40.  
  41.         b=myScanner.nextInt();
  42.  
  43.         if(a>b){
  44.  
  45.             a=a+b;
  46.  
  47.             b=a-b;
  48.  
  49.             a=a-b;
  50.  
  51.         }
  52.  
  53.         System.out.print("Armstrong number are : ");
  54.  
  55.         for(i=a;i<=b;i++){
  56.  
  57.             ck=armstrong(i);
  58.  
  59.             if(ck==i){
  60.  
  61.                 System.out.print(i+"\t");
  62.  
  63.             }
  64.  
  65.         }
  66.  
  67.         System.out.println();
  68.  
  69.     }
  70.  
  71. }
  72.  
  73. //Enter two numbers : 1 1000
  74.  
  75. //Armstrong number are : 1    153    370    371    407