Java Program to Check Armstrong Number
Java Program to Check Armstrong Number
By doing this program you will learn how to check for an 3 digit armstrong number using Java program. This program can be done by using for- loop as well as using while loops. I have used while loop here.Now comes the question, "What is an Armstrong Number? "
A number is called an armstrong number if the sum of the cubes of the digit present in the number is equal to that number.
For eg, if the number is 121. It contains 3 digits- 1,2,1. We will first cube the digits.. Then we add the cubes. If the sum is equal to 121, then 121 is and Armstrong Number. Otherwise, it is not an Armstrong Number.
Examples of Armstrong Number are as follows---
Program:
class Armstrong{
public static void main(int n)
{
int s=0;int k=n;
while(n>0)
{
int r=n%10;
s=s+r*r*r;
n=n/10;
}
if(k==s)
{
System.out.println("Armstrong Number");
}
else
{
System.out.println("Not Armstrong Number");
}
}
}
Output:-
153 Armstrong Number 154 Not Armstrong Number
Java Program to Check Armstrong Number
------------------------------------------------------------
Just copy paste the code into the class file of Bluej compiler. Change the class name according to your's.
Thanks for visting Programming in JAVA.
Happy Coding !!
No comments: