-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy patharmstrong_number.jl
52 lines (43 loc) · 1.05 KB
/
armstrong_number.jl
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
49
50
51
52
""" Julia program to Check if a number is an Armstrong Number or not.
An armstrong number is a number such that the sum of its digits each
raised to the power of the number of digits.
"""
function check_armstrong(num)
temp = num
sum = 0
cnt = floor(log10(num) + 1)
while (temp > 0)
rem = temp % 10
sum = sum + (rem ^ cnt)
temp = temp ÷ 10;
end
if(num == sum)
return true
else
return false
end
end
print("Enter the number: ")
num = readline()
num = parse(Int, num)
if(num < 0)
println("The given number $num is not a Armstrong Number.")
exit()
end
res = check_armstrong(num)
if res
println("The given number $num is a Armstrong Number.")
else
println("The given number $num is not a Armstrong Number.")
end
"""
Time Complexity: O(log(num)), where 'num' is the given number
Space Complexity: O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE 1
Enter the number: 371
The given number 371 is a Armstrong Number.
SAMPLE 2
Enter the number: 1234
The given number 1234 is not a Armstrong Number.
"""