Armstrong Number (JAVA PROGRAM PDF
Armstrong Number (JAVA PROGRAM PDF
/*
* ashking13th@gmail.com
* javaprogramsbyashking13th.blogspot.in
* ashkingjava.blogspot.in
*
* QUESTION
*
* Design a program in java to check whether a number
* is an armstrong number or not.
* An armstrong number is a number in which the sum
* ofcubes of every digit of the number is equal to
* the number itself.
* eg. 153 = 1^3 + 5^3 +3^ 3 = 1+125+27 = 153
*/
import java.io.*;
public class armstrong_number
{
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int n=Integer.parseInt(d.readLine());
int m=n;
//creating dummy variable
int s=0;
int r=0;
while(m>0)
{
r=m%10;
s=s+(r*r*r);
//finding sum of cubes of digits of the number
m=m/10;
}
if(n==s)
/*
* checking if sum of cubes of every digit if the number
* is equal to the number itself and then displaying messages
* accordingly.
*/
{
System.out.println("Number is an armstrong number");
}
else
{
System.out.println("Number is not an armstrong number");
}
}
}
Mar 25, 2014 3:28:21 PM