Q:

Java program to check whether given number is Armstrong or not

belongs to collection: Java Most Popular & Searched Programs

0

The number which is equal to the sum of its digit's cube known as "Armstrong Number", in this Java program, we are going to read an integer number and check whether entered number is Armstrong or not.

Let suppose there is a number 153 (it's an Armstrong number), it is equal to the sum of each it's digit's cube (1*1*1 + 5*5*5 + 3*3*3 = 1+125+27 = 153).

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to check Armstrong number in Java

import java.util.Scanner;

public class ArmStrongNumber {
    
    static boolean isArmStrongNo(int number){
        boolean isArmNumber=false;
        int sum=0;
        int tempNum= number;
        while(tempNum>0){
            int r   =tempNum%10;
            sum     =sum+(r*r*r);
            tempNum =tempNum/10;
        }
        if(sum==number){
            isArmNumber =true; 
        }
        return isArmNumber;
    }
    public static void main(String[] arg){
        Scanner sc  =   new Scanner(System.in);
        System.out.println("Enter Number  : ");
        int num =   sc.nextInt();
        if(isArmStrongNo(num)){
            System.out.println(num+" is an ArmStrong Number");
        }
        else{
            System.out.println(num+" is not an ArmStrong Number");
        }
    }
}

Output

Enter Number  : 153
153 is an ArmStrong Number

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to convert Binary Number into Decimal... >>
<< Java program to find out prime factors of given nu...