Explanation :
Recursion means calling a method within itself until it returns a value.
(If n=153 then Armstrong = 1*1*1 + 5*5*5 + 3*3*3 i.e,
Armstrong =1+125+27=153.)
Code:
import java.util.*;
class armstrong
{
public static void main(String args[])
{
int i,n,sum,m;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number : ");
n=sc.nextInt();
armstrong obj= new armstrong();
m=obj.checknum(n);
if(n==m)
System.out.println("It is a armstrong number");
else
System.out.println("Not a armstrong number");
}
int checknum(int n)
{
if(n==0)
return 0;
else
return (int)Math.pow(n%10,3)+ checknum(n/10);
}
}
No comments:
Post a Comment