Check A Number Is Armstrong or Not Using Java Program

By Saruque Ahamed Mollick

In this post, we are going to learn how to check Armstrong number in Java.

Armstrong Number

Armstrong Number

Just take any number. Take the digits of that number. Now add the cubes of the digits. If the summation is equal to the original number, it is called an Armstrong Number.
Example: 153 is Armstrong number.

(1*1*1)+(5*5*5)+(3*3*3)=1+125+27=153=The number itself

so its Armstrong number

121 is not Armstrong number because



(1*1*1)+(2*2*2)+(1*1*1)=10 is not equal to 121 so it's not Armstrong number.

Java Program To Check A Number is Palindrome or Not

Algorithm of Armstrong Number According to the below program(Java)

Step 1: Take input number from user from Scanner class.

Step 2: Start a while loop until input value reaches zero.” while(n>0)”

Step 3: Add the cube of the “remainder” in each iteration.

Step 4: Finally compare those two numbers. (number entered by user with the number we got at the end of the loop i,e “cube”

Step 5: If the values are same then it will be Armstrong number, else it will not be Armstrong number.

Java Program To Check A Number Is Prime or Not

Java Program of Armstrong Number

import java.util.Scanner;

public class Armstrong {
   
   public static void main(String []args) {
    Scanner input=new Scanner(System.in);
     System.out.println("Enter a Number:");
    int cube=0,remainder,check; 
    int n=input.nextInt();
    check=n;
    int original=n;
    while(n>0)  
    {  
    remainder=n%10;  
    n=n/10;  
    cube=cube+(remainder*remainder*remainder);  
    }  
    if(check==cube)  
    System.out.println(original+" Is Armstrong number");   
    else  
        System.out.println(original+" Is Not Armstrong number");   
   }  
}

Make A Multiplication Table Using Java Program

Output:

 

run:
Enter a Number:
145
145 Is Not Armstrong number
BUILD SUCCESSFUL (total time: 9 seconds)
run:
Enter a Number:
371
371 Is Armstrong number
BUILD SUCCESSFUL (total time: 2 seconds)


Java Program To Get The Sum Of All The Divisors Of A Number


Leave a Reply

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