In this program user will enter any number and the program will show whether the entered number is armstrong number or not. I have used a recursive function to show the result.
import java.io.*;
class Armstrong
{
int n,f=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public void take() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
f=no(n);
if(n==f)
System.out.println(n + " is an armstrong number");
else
System.out.println(n + " is not an armstrong number");
}
int no(int x)
{
int y,z;
if(x!=0)
{
y=x%10;
f=f+y*y*y;
x=x/10;
no(x);
}
return f;
}
public static void main(String args[]) throws Exception
{
Armstrong ob=new Armstrong();
ob.take();
}
}
Related Post: BlueJ Programs on Number
import java.io.*;
class Armstrong
{
int n,f=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public void take() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
f=no(n);
if(n==f)
System.out.println(n + " is an armstrong number");
else
System.out.println(n + " is not an armstrong number");
}
int no(int x)
{
int y,z;
if(x!=0)
{
y=x%10;
f=f+y*y*y;
x=x/10;
no(x);
}
return f;
}
public static void main(String args[]) throws Exception
{
Armstrong ob=new Armstrong();
ob.take();
}
}
Related Post: BlueJ Programs on Number
No comments:
Post a Comment