Wednesday, January 16, 2008

Armstrong Number

Here is a Program to find out if a number is armstrong or not

import java.io.*;
class armstrongno
{
public static void main(String args[])throws IOException
{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int d,n,dup,sum=0;
System.out.println("Enter a NO.:");
n = Integer.parseInt(br.readLine());
for(dup=n;n>0;n/=10)
{
d=n%10;
sum = sum + d*d*d;
}
if (sum==dup)
System.out.println(dup+ " is an armstrong no.");
else
System.out.println(dup+ " is not an armstrong no.");
}
}

1 comment:

hitesh kumar said...

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