×

Java Programs

Java Practice

Java program to check whether given number is Armstrong or not

What is Armstrong Number?

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.

Example

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).

Java program to check whether given number is Armstrong or not

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

Java Most Popular & Searched Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.