Sunday, 7 July 2013

To Check Armstrong Number in Java

/*
Java Prog to check whether given number is Armstrong or not

Hint-> when sum of cubes of individual digit is equal to the numbr itself
       Example 153 = 1^3+5^3+3^3;

*/


import java.io.*;


class Armstrong
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

int rem,sum,cpy;

System.out.println("ENTER THE NUMBER TO BE CHECKED");
int num = Integer.parseInt(input.readLine());

cpy=num;
sum=0;

while(num!=0)
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(sum==cpy)
System.out.println("THE NUMER "+cpy+" IS AN AMSTRONG NUMBER");
else
System.out.println("THE NUMER "+cpy+" IS NOT AN AMSTRONG NUMBER");
}
}

No comments:

Post a Comment