Thursday, 16 June 2016

Program to know whether a number is Armstrong or not.

Explanation :
If n=153 then Armstrong = 1*1*1 + 5*5*5 + 3*3*3 i.e,
Armstrong =1+125+27=153.

Code:

import java.util.Scanner;
public class Armstrong
{
    public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       int num = sc.nextInt(); 
       int n = num; 
       int arm=0,remainder; 
       while(num > 0)
       {
           remainder = num % 10; 
           arm = arm + (int)Math.pow(remainder,3); 
           num = num / 10; 
        } 
           if(arm == n) System.out.println(n+" is an Armstrong Number"); 
           else System.out.println(n+" is not a Armstrong Number"); 
     }
}

No comments:

Post a Comment