armstrong number in java using recursion

Solutions on MaxInterview for armstrong number in java using recursion by the best coders in the world

showing results for - "armstrong number in java using recursion"
Greta
29 Jan 2016
1// Armstrong number in java using recursion
2import java.util.Scanner;
3class RecursionArmstrong
4{
5   int number, sum;
6   RecursionArmstrong(int num1)
7   {
8      number = num1;
9      sum = 0;
10   }
11   long powerOf(int a, int b)
12   {
13      if(b == 0)
14      {
15         return 1;
16      }
17      else
18      {
19         return a * powerOf(a, b - 1);
20      }
21   }
22   void executePowerOf()
23   {
24      int d = number, r;
25      long total = (long)sum;
26      while(d > 0)
27      {
28         r = d % 10;
29         d = d / 10;
30         total = total + powerOf(r, 3);
31      }
32      if(number == total)
33      {
34         System.out.println("Given number is Armstrong number.");
35      }
36      else
37      {
38         System.out.println("Not an Armstrong number.");
39      }
40   }
41   public static void main(String[] args) 
42   {
43      Scanner sc = new Scanner(System.in);
44      System.out.print("Please enter the number: ");
45      int num = sc.nextInt();
46      RecursionArmstrong obj = new RecursionArmstrong(num);
47      obj.executePowerOf();
48      sc.close();
49   }
50}