codescracker


java

Java Program to Check Armstrong or Not



« Previous Program Next Program »


Check Armstrong or Not

To check whether a number is an Armstrong number or not Armstrong number in Java Programming, you have to ask to the user to enter the number, now check whether the entered number is an Armstrong number or not as shown in the following program.

To check whether any positive number is an Armstrong number or not, you have to perform the summation of, three times multiplication of, all the digits present in the number, if the summation result is equal to the actual number then the number will be an Armstrong number, otherwise the number will not be an Armstrong number, following is the example :

Java Programming Code to Check Armstrong or Not

Following Java Program ask to the user to enter any positive integer to find whether it is an Armstrong number or not, then display the result on the screen:

/* Java Program Example - Check Armstrong or Not */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int n, nu, num=0, rem;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter any Positive Number : ");
        n = scan.nextInt();
		
        nu = n;
		
        while(nu != 0)
        {
            rem = nu%10;
            num = num + rem*rem*rem;
            nu = nu/10;
        }
        if(num == n)
        {
            System.out.print("Armstrong Number");
        }
        else
        {
            System.out.print("Not an Armstrong Number");
        }
    }
}

When the above Java Program is compile and executed, it will produce the following output. Above Java Programming Example Output (Armstrong Number):

Java Program find armstrong number

Above Java Programming Example Output (Not Armstrong Number):

Java Program check armstrong or not

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test