Java program to check Armstrong number

Here is a Java program to check the number is Armstrong number or not. An Armstrong number, also known as narcissistic number, is a number that is equal to the sum of the cubes of its own digits.

For example, 153 is an Armstrong number since 153 = 1*1*1 + 7*7*7 + 0*0*0.

Source Code:

//java program to check Armstrong number
import java.util.Scanner;

public class Armstrong {
  public static void main(String args[]) {
    int aNum = 0, r, num, cNum;
		
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter no. to check:");
    num = sc.nextInt();
    cNum = num;
		
    do {
      r = cNum % 10;
      cNum = cNum / 10;
      aNum = aNum + r*r*r;
    }while(cNum!=0);
		
    if(num == aNum) {
      System.out.println("Entered number is Armstrong number.");
    }
    else {
    System.out.println("Entered number is not Armstrong number.");
    }
  }	
}

Input & Output:

Enter no. to check:22
Entered number is not Armstrong number.

OR

Enter no. to check:153
Entered number is Armstrong number.

Recommended Post