forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmStrongRecursion.java
More file actions
33 lines (30 loc) · 848 Bytes
/
ArmStrongRecursion.java
File metadata and controls
33 lines (30 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Checking Armstrong number with Recursion
import java.util.Scanner;
class ArmStrongRecursion
{
static int p;
public static int armstrong(int i)
{ if(i<10) //base class
return (int)(Math.pow(i,p));
else
return((int)Math.pow(i%10,p)+armstrong(i/10));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("N = ");
int n=sc.nextInt();
p=Integer.toString(n).length(); //calculating the number of digits in the number by converting it into String
if(n==armstrong(n))
System.out.println(n+" is an Armstrong number");
else
System.out.println(n+" is not an Armstrong number");
}
}
/*
Sample Input and Output :
N = 153
153 is an Armstrong number
Space Complexity: O(1)
Time Complexity : O(p)
*/