Java program to check whether the given number is an Armstrong number



An Armstrong number is a number which equals to the sum of the cubes of its individual digits. For example, 153 is an Armstrong number as −

153 = (1)3 + (5)3 + (3)3
153 1 + 125 + 27
154 153

Algorithm

1. Take integer variable Arms
2. Assign value to the variable
3. Split all digits of Arms
4. Find cube-value of each digits
5. Add all cube-values together
6. Save the output to Sum variable
7. If Sum equals to Arms print Armstrong Number
8. If Sum not equals to Arms print Not Armstrong Number

Example

import java.util.Scanner;
public class ArmstrongNumber {
   public static void main(String args[]) {
      int number = 153;
      int check, rem, sum = 0;
      System.out.println("Enter the number to be verified:");
      Scanner sc = new Scanner(System.in);
      number = sc.nextInt();
      check = number;
      while(check != 0) {
         rem = check % 10;
         sum = sum + (rem * rem * rem);
         check = check / 10;
      }
      if(sum == number)
         System.out.println("Given number is an armstrong number.");
      else
         System.out.println("Given number is not an armstrong number.");
   }
}

Output

Enter the number to be verified:
153
Given number is an armstrong number.
Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician


Advertisements