Here is the program to check a number whether it is Armstrong
number or not using while loop.
import java.io.*;
class A
{
int n;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
public void take() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
if(no(n))
System.out.println(n + " is an armstrong
number");
else
System.out.println(n + " is not an armstrong
number");
}
private boolean no(int x)
{
int f=0,y,m;
m=x;
while(x>0)
{
y=x%10;
f=f+y*y*y;
x=x/10;
}
if(m==f)
return true;
else
return false;
}
public static void main(String args[]) throws Exception
{
A ob=new A();
ob.take();
}
}
No comments:
Post a Comment