Monday, July 16, 2012

BlueJ Program On Displaying Armstrong Number Using Recursive Function

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 PostBlueJ Programs on Number

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner