forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckArmstrongNumber.java
48 lines (42 loc) · 913 Bytes
/
CheckArmstrongNumber.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// JAVA program to find Armstrong
// numbers between two integers
import java.io.*;
import java.math.*;
class gfg {
// Function to print Armstrong
// Numbers between two integers
static void ArmstrongNum(int l, int h)
{
for (int j = l + 1; j < h; ++j) {
// Calculating number of digits
int y = j;
int N = 0;
while (y != 0) {
y /= 10;
++N;
}
// Calculating the sum of nth
// power of all the digits
int sum_power = 0;
y = j;
while (y != 0) {
int d = y % 10;
sum_power += Math.pow(d, N);
y /= 10;
}
// Checking if the current number
// i is equal to the sum of nth
// power of all the digits
if (sum_power == j)
System.out.print(j + " ");
}
}
// The Driver code
public static void main(String args[])
{
int n1 = 50;
int n2 = 500;
ArmstrongNum(n1, n2);
System.out.println();
}
}