Wednesday, February 8, 2017

Java Program to check a number whether it is an armstrong number or not.

Note: An Armstrong number can calculated as follows.
Step1: Enter a Number[153]
Step2: Count the total digit of number[3]
Step3: Power of each digit to the total no. of digit and sum[(3*3*3)+(5*5*5)+(1*1*1)=153]
Step4: Compare the entered no[153] and getting no[153]. if matched then armstrong no. otherwise no.
*/
import static java.lang.System.out;
import java.io.Console;
class Armstrong
{
static public void main(String...alt)
{
Console c=System.console();
String s=c.readLine("Enter a Number : ");
int n=Integer.parseInt(s);
int temp=n;
int rem=0,sum=0;
while(n!=0)
{
rem=n%10;
int pow=(int)Math.pow(rem,s.length());
sum=sum+pow;
out.println(sum);
n=n/10;
}
if(temp==sum)
out.println(temp+" is Armstrong No");
else
out.println(temp+" is Not Armstrong No");
}
}

Share this

0 Comment to "Java Program to check a number whether it is an armstrong number or not."

Post a Comment