Input a number and check it is an armstrong number or not using while loop in Java
while Loop - Question 15
In this question, we will see how to input a number and check if it is an armstrong numbers or not in Java programming using while loop. To know more about while loop click on the while loop lesson.
Q15) Write a program in Java to input a number and check if it is an armstrong numbers or not using while loop.
An Armstrong number is a number whose sum of cube of the digits is equal to the original number. Example 153 = 13 + 53 + 33
Program
import java.util.Scanner;
public class Q15
{
public static void main(String args[])
{
int n,d,on,soc=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any number ");
n=sc.nextInt();
on=n; // storing the original number
while(n>0)
{
d=n%10;
soc=soc+(d*d*d); // sum of cube of the digits
n=n/10;
}
if(on==soc)
{
System.out.println("Armstrong Number");
}
else
{
System.out.println("Not Armstrong Number");
}
}
}
Output
Enter any number 153 Armstrong Number