Write a Program to check number is Armstrong number or not

public class Armstrong{
     public static void main(String []args){
        int n = 153;
        int temp=0,rem,number;
        number  = n;
        while(number > 0)
        {
            rem = number % 10;
            temp = temp + rem * rem * rem; 
            number = number / 10;
        }
        System.out.println("Temp = " + temp);
        if(n == temp)
        System.out.println("Armstrong number");
        else
        System.out.println("Not Armstrong number");
        
     }
}

Example: – If you have three digits number then it will calculate like below

153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153

If you have a four-digit number then it will calculate like below

1634 = 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1 + 1296 + 81 + 256 = 1634

Output: –

Temp = 153
Armstrong number

Leave a Reply

Your email address will not be published. Required fields are marked *