Armstrong Number in Java

Table of Contents

Armstrong Number in Java

What is Armstrong Number ?

In number theory, a number in a given number base is a number that is the sum of its own digits each raised to the power of the number of digits. For instance, 153 is an Armstrong Number as it is equal to the sum of its individual cubes of digits, which is 13 + 53 + 33. Also, 370,371,407 are examples of Armstrong Number.

Compile and Run the following code with Command-Prompt Or download Notepad++

For Compilation javac Sample.java

For Execution java Sample

import java.util.Scanner;
class Sample
{
  public static void main(String args[])
  {
    int num,temp,sum,i = 1,n = 0;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of your choice: ");
    a = sc.nextInt();
    sum = 0;
    i = num;
    while(num > 0)
    {
      n =num%10;
      num = num/10;
      sum =sum + (n*n*n)
    }
    if(sum == i)
      System.out.println("Is an Armstrong Number");
    else
      System.out.println("Is not an Armstrong Number");
  }
}

Leave a Reply