-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_10.py
More file actions
61 lines (35 loc) · 1.42 KB
/
3_10.py
File metadata and controls
61 lines (35 loc) · 1.42 KB
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
53
54
55
56
57
58
59
60
61
"""
10.
(a) One way to find out the last digit of a number is to mod the number by 10. Write a
program that asks the user to enter a power. Then find the last digit of 2 raised to that
power.
(b) One way to find out the last two digits of a number is to mod the number by 100. Write
a program that asks the user to enter a power. Then find the last two digits of 2 raised to
that power.
(c) Write a program that asks the user to enter a power and how many digits they want.
Find the last that many digits of 2 raised to the power the user entered.
"""
print('a.)')
power = eval(input('Enter power:'))
result = 2 ** power
last = result % 10
print('Result:',result,',','Last Digit:',last)
print()
#######################################################################
print('b.)')
power = eval(input('Enter power:'))
result = 2 ** power
last = result % 100
print('Result:',result,',','Last Two Digits:',last)
print()
######################################################################
print('c.)')
power = eval(input('Enter power:'))
num_digit = eval(input('Enter number of digit:'))
result = 2 ** power
last = result % (10 ** num_digit)
if( len(str(last)) != num_digit ):
leading_zero = num_digit - len(str(last))
print('Result:',result,',' , 'Last ' , num_digit,' Digits:' , leading_zero * '0',last,sep='')
else:
print('Result:',result,',','Last', num_digit ,'Digits:',last)