-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.py
More file actions
42 lines (33 loc) · 775 Bytes
/
loops.py
File metadata and controls
42 lines (33 loc) · 775 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
34
35
36
37
38
39
40
41
42
# Reverse a Number (No String)
num = 12345
#storing a number in a variable called num
reverse = 0
#another variable called reverse. start from 0
while num > 0:
#loop continues as long as number still has digits
digit = num % 10
reverse = reverse * 10 + digit
num = num // 10
print(reverse)
# armstrong num
num = 153
original = num
sum = 0
while num > 0:
digit = num % 10
sum += digit * digit * digit
num //= 10
if sum == original:
print("Armstrong")
else:
print("Not Armstrong")
# second largest
arr = [10, 20, 4, 45, 99]
largest = second = -999999
for num in arr:
if num > largest:
second = largest
largest = num
elif num > second and num != largest:
second = num
print("Second Largest:", second)