Given a number the task is to check whether it is Armstrong Number or not using recursion.
Armstrong Number is a number whose sum of the power of digits is equal to the original number where power is number of digits in the number.
Example 1: 153. 13+53+33 = 1+125+27 = 153.
Example 2: 1634. 14+64+34+44 = 1+1296+81+256 = 1634.
Java
import java.util.*;
public class ArmstrongRec
{
public int getCount(int n)
{
if(n==0)
{
return 0;
}
else
{
return 1+getCount(n/10);
}
}
public int getPowerSum(int n,int count)
{
if(n==0)
{
return 0;
}
else
{
return (int)Math.pow((n%10),count)+getPowerSum(n/10,count);
}
}
public static void main(String args[])
{
int n=0,count=0;
Scanner sc=new Scanner(System.in);
ArmstrongRec ob=new ArmstrongRec();
System.out.print("Enter a number: ");
n=sc.nextInt();
count=ob.getCount(n);
if(ob.getPowerSum(n,count)==n)
{
System.out.println(n+" is a Armstrong Number");
}
else
{
System.out.println(n+" is not a Armstrong Number");
}
}
}Java