Here's a Java program to find the given number is Armstrong or not with output and proper explanation. This program also uses Java while loop and modulus.
Output of above program is
Enter a number : 153
The no. is Armstrong.
What is an Armstrong Number? A number is an Armstrong number if the sum of cubes of each digit of that number is equal to the number itself e.g. 153 is an Armstrong number i.e. 13 + 53 + 33 = 153.
import java.io.*; class armstrong{ public static void main(String [] args) throws IOException{ try { BufferedReader obc=new BufferedReader (new InputStreamReader(System.in)); int n1,r,n2,arm=0; System.out.println("Please enter the number: "); n1=Integer.parseInt(obc.readLine()); n2=n1; while(n1>0) { r=n1%10; arm=arm+r*r*r; n1=n1/10; } if(n2==arm) { System.out.println("The no. is Armstrong."); } else { System.out.println("The no. is not Armstrong."); } }catch(IOException e) { System.out.println("The entered no. is wrong"); } } }
Output of above program is
Enter a number : 153
The no. is Armstrong.