IQ

Ad SpeedyAds

Wednesday, October 3, 2012

Java porgram to check whether a number is Armstrong number

//program for checking Armstrong Number
//An Armstromg number is one which is equal to the sum of the cubes of its digits
//Eg. 153=1³+5³+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.");
}
}

2 comments:

  1. this program is not true for numbers with more or less than 3 digits

    ReplyDelete