Tuesday, October 2, 2012

Java program to check if a number is Armstrong or not.

//program for checking Armstrong Number
//An Armstrong number is one which is the sum of cube of all its digits
//Eg. 153=1*1*1+5*5*5+3*3*3=1+125+27=153..
import java.io.*;
class armstrong
{
 public static void main(String aa[]) throws IOException
{
 BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a, b=0, c, d;
System.out.println("Enter a number to check for Armstong");
String S=br.readLine();
a=Integer.parseInt(S);
c=a;
while(c!=0)
{
 d=c%10;
 c=c/10;
 b+=d*d*d;
}
if(a==b)
System.out.println(a+" is an Armstrong no.");
else
System.out.println(a+" is NOT an Armstrong no.");
}
}

1 comment:

  1. Armstrong Program in Java

    In general, Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself.
    Simple example of armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153

    ReplyDelete