Automation Using Selenium Webdriver
Showing posts with label Java programs examples. Show all posts
Showing posts with label Java programs examples. Show all posts

Wednesday, 7 September 2016

Java programs

Palindrome number algorithm

  • Get the number to check for palindrome
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print "palindrome number"
  • Else print "not palindrome number"
  • class PalindromeExample{  
  •  public static void main(String args[]){  
  •   int r,sum=0,temp;    
  •   int n=454;//It is the number variable to be checked for palindrome  
  •   
  •   temp=n;    
  •   while(n>0){    
  •    r=n%10;  //getting remainder  
  •    sum=(sum*10)+r;    
  •    n=n/10;    
  •   }    
  •   if(temp==sum)    
  •    System.out.println("palindrome number ");    
  •   else    
  •    System.out.println("not palindrome");    
  • }  
  • }  
  • out put:
  • palindrome  number

Factorial Program using loop in java

  1. class FactorialExample{  
  2.  public static void main(String args[]){  
  3.   int i,fact=1;  
  4.   int number=5;//It is the number to calculate factorial    
  5.   for(i=1;i<=number;i++){    
  6.       fact=fact*i;    
  7.   }    
  8.   System.out.println("Factorial of "+number+" is: "+fact);    
  9.  }  
  10. }  
  11. out put:Factorial of 5 is: 120

Prime Number Program in Java

Prime number in Java: Prime number is a number that is greater than 1 and divided 
by 1 or itself. In other words, prime numbers can't be divided by other 
numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

  1. class PrimeExample{  
  2.  public static void main(String args[]){  
  3.   int i,m=0,flag=0;    
  4.   int n=17;//it is the number to be checked  
  5.   m=n/2;    
  6.   for(i=2;i<=m;i++){    
  7.    if(n%i==0){    
  8.    System.out.println("Number is not prime");    
  9.    flag=1;    
  10.    break;    
  11.    }    
  12.   }    
  13.   if(flag==0)    
  14.   System.out.println("Number is prime");    
  15. }  
  16. }  
  17. Output:Number is prime

Armstrong Number in Java: Armstrong number is a number that is equal
 to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.
Let's try to understand why 153 is an Armstrong number.

  1. 153 = (1*1*1)+(5*5*5)+(3*3*3)  
  2. where:  
  3. (1*1*1)=1  
  4. (5*5*5)=125  
  5. (3*3*3)=27  
  6. So:  
  7. 1+125+27=153

  1. class ArmstrongExample{  
  2.   public static void main(String[] args)  {  
  3.     int c=0,a,temp;  
  4.     int n=153;//It is the number to check armstrong  
  5.     temp=n;  
  6.     while(n>0)  
  7.     {  
  8.     a=n%10;  
  9.     n=n/10;  
  10.     c=c+(a*a*a);  
  11.     }  
  12.     if(temp==c)  
  13.     System.out.println("armstrong number");   
  14.     else  
  15.         System.out.println("Not armstrong number");   
  16.    }  


Compare String with/without using equals() method.



mport java.util.Scanner;

public class CompareString{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the 1st String: ");
        String s1 = in.next();
        System.out.println("Enter the 2nd String: ");
        String s2 = in.next();

        //1st approach without using equals() method
        System.out.println("*********compare  1st String ************");
        if(s1.length()==s2.length()){
            for(int i=0; i<s1.length(); i++){
                if(s1.charAt(i)!=s2.charAt(i)){
                    System.out.println("String "+s1+" is not equal to string "+s2);
                    break;
                }
            }
            System.out.println("String "+s1+" is equal to string "+s2);
        }else{
            System.out.println("String "+s1+" is not equal to string "+s2);
        }

        //2nd approach , just use equals() method
        System.out.println("*********compare 2nd String************");
        if(s1.equals(s2)){
            System.out.println("String "+s1+" is equal to string "+s2);
        }else{
            System.out.println("String "+s1+" is not equal to string "+s2);
        }    
    }
}
OutPut
-------------

Enter the 1st String: 
Selenium Webdriver
Enter the 2nd String: 
Selenium Webdriver
*********compare  1st String************
String Selenium Webdriver is equal to string Selenium Webdriver
*********compare  2nd String************
String Selenium Webdriver is equal to string Selenium Webdriver

Flyod Trinagle


Note- Floyd Triangle is like
1
2 3
4 5 6
7 8 9 10
------------
Code-

import java.util.Scanner;

public class FloydTriangle{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
        int r = in.nextInt();
        int n=0;
        for(int i=0; i<r; i++){
            for(int j=0; j<=i; j++){
                System.out.print(++n+" ");
            }
            System.out.println();
        }
    }
}

Output

Enter the number of rows which you want in your Floyd Triangle: 
5

2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15