3) user input number: 123456 -> 56 34 12
6) user input number: 123456 -> Sum of 56 34 12
Program names:
Reverse Number in Pairs
Sum of 2-digit groups after reversing the groups
n = int(input("Enter the number: "))
total = 0
while n > 0:
m = n % 100
n = n // 100
print(m)
total += m
print(f'Sum of 2-digit groups = {total}')
Output:
Enter the number: 123456
56
34
12
Sum = 102
====================================================================
4) user input number: 123456 -> 456 123
7) user input number: 123456 -> Sum of 456 123
Program names:
Halves of a Number
Sum of Halves of a Number
n = int(input("Enter the number: "))
total = 0
while n > 0:
m = n % 1000
n = n // 1000
print(m)
total += m
print(f'Sum of 3-digit groups = {total}')
Output:
Enter the number: 123456
456
123
Sum of 3-digit groups = 579
====================================================================
5) user input number: 123456 -> 56 45 34 23 12
Program name:
Print Overlapping Two-Digit Pairs
n = int(input("Enter the number: "))
total = 0
while n > 1:
m = n % 100
n = n // 10
print(m)
total += m
print(f'Sum of Overlapping Two-Digit Pairs = {total}')
Output:
Enter the number: 123456
56
45
34
23
12
Sum of Overlapping Two-Digit Pairs = 170
====================================================================
1) user input number: 123456 -> 1+3+5
Method 1:
n = 123456
m = n // 100000
n = n % 10000
print(m)
m = n // 1000
n = n % 100
print(m)
m = n // 10
n = n % 1
print(m)
Method 2:
n = 123456
div = 100000
total = 0
while div > 0:
m = n // div
n = n % (div //10)
total += m
print(m)
div = div // 100
print(f'total = {total}')
Output:
1
3
5
total = 9
====================================================================
2) user input number: 123456 -> 2 + 4 + 6
Method 1:
n = 123456
n = n % 100000
m = n // 10000
print(m)
n = n % 1000
m = n // 100
print(m)
n = n % 10
m = n // 1
print(m)
Method 2:
n = 123456
div = 100000
total = 0
while div > 0:
n = n % div
m = n // (div//10)
total += m
print(m)
div = div // 100
print(f'total = {total}')
Output:
2
4
6
total = 12
Top comments (0)