Write a program to find the given number is Armstrong number or not with the help of a recursive function.

Armstrong number is a number that is equal to the sum of digits raised to the power as length of the number.

import java.io.*;
public class armnum
{
    static double n;int l;
    void armnum(int nn)
    {
        n=nn;
        String str=Double.toString(n);
        l=str.length();
        
}
    double sum_pow(double i)
    {
        if(i==0)
        return 0;
        else
        {
            double d=i%10;
            double sum=Math.pow(d,l);
            return(sum+sum_pow(i/10));
        }
    }
    void isarmstrong()
    {
        if(sum_pow(n)==n)
        System.out.println("armstrong number");
        else
        System.out.println("not armstrong number");
    }
    public static void main()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the number");
        double n=Double.parseDouble(br.readLine());
        armnum obj=new armnum();
        obj.isarmstrong();
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here