• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Palindrome Number in Java
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. It can also be a string like LOL, MADAM etc.
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.

public class Main 
{

    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
Armstrong Number in Java
Armstrong Number in Java: A positive number is called armstrong number if it 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

public class Main 
{

    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
Fibonacci Series in Java
Fibonacci Series in C: In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.

public class Main 
{

    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
Check the given number is Prime Number or Not

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, 19, 23.... are the prime numbers.

import java.util.*;
public class Demo 
{

    public static void main(String[] args) 
    {
    
          int i,m=0,flag=0,n;      
          Scanner s=new Scanner(System.in);
          System.out.println("Enter n Value");
          n=s.nextInt();
          m=n/2;      
          if(n==0||n==1)
          {  
           System.out.println(n+" is not prime number");      
          }
          else
          {  
           for(i=2;i<=m;i++)
           {      
            if(n%i==0)
            {      
             System.out.println(n+" is not prime number");      
             flag=1;      
             break;      
            }      
           }      
           if(flag==0)  
           { 
               System.out.println(n+" is prime number"); 
           }  
          }//end of else  
    }    
}   


                                

Output:
Enter n Value
3
3 is prime number

Enter n Value
10
10 is not prime number