Are You Looking For JAVA/J2EE Online Training ?

Fill in your details in below form, we will get back to you

Powered by Blogger.

Hadoop Online Training With Placement in USA

www.itlearnmore.com/special-offer-java-5-for-1-package

Limited Offer


Be an expert at low cost

Core Java and Adv JAVA, Struts, HTML and SQL Server Video courses for $20..... Click here


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

Thursday, 30 April 2015

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.

Note: 0 and 1 are not prime numbers. 2 is the only even prime number because all the numbers can be divided by 2.

Let's see the prime number program in java. In this java program, we will take a number variable and check whether the number is prime or not.

class PrimeExample{  
 public static void main(String args[]){  
 int i,m=0,flag=0;    
  int n=17;//it is the number to be checked  
  m=n/2;    
  for(i=2;i<=m;i++){    
   if(n%i==0){    
   System.out.println("Number is not prime");    
   flag=1;    
   break;    
   }    
  }    
  if(flag==0)    
  System.out.println("Number is prime");    
}  
}  


Palindrome number in java: A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers.

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"
Let's see the palindrome program in java. In this java program, we will get a number variable and check whether number is palindrome or not.

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");    
}  
} 

Output:
palindrome  number


In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program in java:
  • Fibonacci Series without using recursion
  • Fibonacci Series using recursion
Fibonacci Series in Java without using recursion

Let's see the fibonacci series program in java without using recursion.

class FibonacciExample1
{  
public static void main(String args[])  
{    
 int n1=0,n2=1,n3,i,count=10;    
 System.out.print(n1+" "+n2);   //printing 0 and 1    
    
 for(i=2;i<count;++i)
//loop starts from 2 because 0 and 1 are already printed   
                   {    
                             n3=n1+n2;    
                             System.out.print(" "+n3);    
 n1=n2;    
                             n2=n3;    
}    
  
}
}  
Output:
0 1 1 2 3 5 8 13 21 34


Fibonacci Series using recursion in java
Let's see the fibonacci series program in java using recursion.
class FibonacciExample2{  
 static int n1=0,n2=1,n3=0;    
static void printFibonacci(int count){    
    if(count>0){    
        n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         System.out.print(" "+n3);   
         printFibonacci(count-1);    
     }    
 }    
 public static void main(String args[]){    
  int count=10;    
  System.out.print(n1+" "+n2);//printing 0 and 1    
  printFibonacci(count-2);//n-2 because 2 numbers are already printed   
 }  
}  
Output:
0 1 1 2 3 5 8 13 21 34


Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

                               1.    4! = 4*3*2*1 = 24  
                               2.    5! = 5*4*3*2*1 = 120  
Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in java language. Let's see the 2 ways to write the factorial program in java.
o    Factorial Program using loop
o    Factorial Program using recursion

Factorial Program using loop in java
Let's see the factorial Program using loop in java.
class FactorialExample{  
 public static void main(String args[]){  
  int i,fact=1;  
  int number=5;//It is the number to calculate factorial    
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  
Output:
Factorial of 5 is: 120



Factorial Program using recursion in java

Let's see the factorial program in java using recursion.

class FactorialExample2{  
 static int factorial(int n){    
  if (n == 0)    
    return 1;    
  else    
    return(n * factorial(n-1));    
 }    
 public static void main(String args[]){  
  int i,fact=1;  
  int number=4;//It is the number to calculate factorial    
  fact = factorial(number);   
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  


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.

153 = (1*1*1)+(5*5*5)+(3*3*3)  
where:  
(1*1*1)=1  
(5*5*5)=125  
(3*3*3)=27  
So:  
1+125+27=153  
Let's try to understand why 371 is an Armstrong number.

371 = (3*3*3)+(7*7*7)+(1*1*1)  
where:  
(3*3*3)=27  
(7*7*7)=343  
(1*1*1)=1  
So:  
27+343+1=371  
Let's see the java program to check Armstrong Number.

class ArmstrongExample{  
  public static void main(String[] args)  {  
    int c=0,a,temp;  
    int n=153;//It is the number to check armstrong  
    temp=n;  
    while(n>0)  
    {  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("armstrong number");   
    else  
        System.out.println("Not armstrong number");   
   }  
}  
Output:
armstrong number