Armstrong Number Java Program

By | January 9, 2016

2 Ways To Find Armstrong Number in Java

Let us understand the concept of Armstrong Number in Java programming language and write a Java program to check Armstrong number using a variety of methods.

What is Armstrong Number Series?

An Armstrong integer is the sum of cubes of the individual digits of the integer. If the sum of the digits is equal to the number, then it is an Armstrong Number.

An Armstrong number is similar to a Narcissistic Number. There’s a difference between an Armstrong number and a Narcissistic number.

An Armstrong integer is usually a 3 digit number whereas a narcissistic number which consists of 4 or more than 4 digits, which is also regarded as 4 digit Armstrong Number.

Algorithm For Armstrong Numbers

Step 1: Start
Step 2: Input num from the user, assign temp =  num and sum = 0
Step 3: while(num > 0) 
        do
              rem = num % 10
              sum = sum + (rem * rem * rem)
              num1 = num / 10
        Else go to Step 5
Step 4: Go to Step 3
Step 5: If (temp == sum) Then
              Print Armstrong Number
        Else 
              Print Not an Armstrong Number
        EndIf
Step 6: Stop

Must Read: Java Program To Print Map of India

Example of Armstrong Number

153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3)
407 = (4 * 4 * 4) + (0 * 0 * 0) + (7 * 7 * 7)
371 = (3 * 3 * 3) + (7 * 7 * 7) + (1 * 1 * 1)

Armstrong Number Formula

Armstrong Number Formula in Mathematical Expression for Calculation Armstrong in Java Programming

Note: This Java program for Armstrong Number series is compiled on Linux Ubuntu 14.04 and written using gEdit editor. However, this Armstrong Number Java program is compatible with all other operating systems.

Method 1: Java Program To Check Armstrong Number using While Loop

import java.io.*;

class Armstrong
{
    public static void main(String args[]) throws Exception
    {
        int num, temp, sum = 0, rem = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\nEnter an Integer:");
        num = Integer.parseInt(br.readLine());
        temp = num;
        while(num > 0)
        {
            rem = num%10;
            sum = sum + (rem * rem * rem);
            num = num/10;
        }
        if(temp == sum)
        {
            System.out.println("\n" + temp + " is an Armstrong Number\n");
        }
        else
        {
            System.out.println("\n" + temp + " is Not an Armstrong Number\n");
        }
    }
}

Must Read: Java Program To Find Palindrome Numbers

 

Method 2: Simple Program For Armstrong Number in Java using While Loop

import java.io.*;

class Armstrong
{
      public static void main(String args[]) throws Exception
      {
            int num, temp, sum = 0, rem = 0;      
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("\nEnter an Integer:");
            num = Integer.parseInt(br.readLine());
            temp = num;
            for(; num > 0; num = num / 10)
            {
                  rem = num%10;
                  sum = sum + (rem * rem * rem);
            }
            if(temp == sum)
            {
                  System.out.println("\n" + temp + " is an Armstrong Number\n");
            }
            else
            {
                  System.out.println("\n" + temp + " is Not an Armstrong Number\n");
            }
      }
}

Must Read: Java Program To Print System Date and Time

 

Output

Find Armstrong Number in Java Programming using While and For Loop

Let’s discuss more on Armstrong number in Java programming in the comment section below if you have any compilation errors and any doubts about the same. For more information on Armstrong series, refer Wikipedia.

Let's Discuss