How to check Armstrong number in java

Checking Armstrong number

 

Steps to do this:-  
1.take n,i,sum=0,r. we have to initialize sum with 0. n is for user input .i is for storing n's value . r is for storing remainder.
2.store the user value in i
 3.start while loop (n>0) and do this
r=n%10 it will take last value from the number (if 153 is entered then first time r will store 3)
n=n/10, it will eliminate the last number ( n is now 15)
sum=sum+(r*r*r) (sum=0+3*3*3*)
4.Check if  (sum==i) then print "this an Armstrong  number" 
else
print "this is not an Armstrong  number"
5. save and run
The Source Code
import java.util.Scanner;
class arm
{
  public static void main(String[] args) 
{
   Scanner s=new Scanner(System.in);
        int n,i,r,sum=0;// n is for input i is for storing n's value r is for remainder sum is for ans
        System.out.println("enter the number");
        n=s.nextInt();
        i=n;
        while(n>0)
        {
            r=n%10;
            n=n/10;
            sum=sum+(r*r*r);
        }
        if(sum==i)
        {
            System.out.println("this is an armstrong number");
        }
        else
        {
            System.out.println("this is not an armstrong number");
        }
    }
}
OUTPUT
WATCH MY VIDEO

Comments

Popular Posts