-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.py
90 lines (82 loc) · 2.25 KB
/
7.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Write a Python program which takes a number and prints the digits from the unit place,
# then the tenth, then hundredth, etc. (Right to Left)
# [Consider the input number to be an INTEGER. You are not allowed to use String indexing for solving this task]
# Example: If the user gives 32768, then print 8, 6, 7, 2, 3''')
#Solve: using for loop
x = input("Please enter a number: ")
xx = int(x)
for i in x:
y = xx % 10
xx = xx // 10
if xx == 0:
print(y, end = " ")
else:
print(y, end = ", ")
#solve: using while loop
x = 32768
while x:
print(x%10)
x //= 10
#was thinking differently:
val = int(input("Enter the digit: "))
lst = []
for i in range(len(str(val))):
if i == 0:
ans = val% 10
lst.append(ans)
rem = val // 10
if i>0:
ans = rem % 10
lst.append(ans)
rem = rem // 10
for x in lst:
if x == lst[-1]:
print(x, end=' ')
else:
print(x, end = ', ')
#2)Write a Python program that takes a number and prints how many digits are in that number. [Consider the input number to be an INTEGER.] [You are not allowed to use len() function]
#Example: If the user gives 9876, your program should print 4.
#solve: while loop
x = int(input("enter:"))
y = 0
while x:
x = x//10
y = y + 1
print(y)
#solve: for loop
x = input("Enter: ")
xx = int(x)
y = 0
for i in x:
xx = xx// 10
y = y + 1
print(y)
#3)** Write a Python program that takes a number from the user and prints its digits from left to right. [Consider the input number to be an INTEGER. You are not allowed to use String indexing for solving this task]
#Example: if the user gives 32768, then print 3, 2, 7, 6, 8
#solve: using for loop
y = input("Enter:")
yy = int(y)
ylen = len(y)
q = 10 ** (ylen - 1)
for i in y:
z = yy // q
yy = yy % q
q = q // 10
if yy == 0:
print(z, end = " ")
else:
print(z, end = ", ")
#solve: using while loop
y = int(input("enter:"))
p = 0 #p is the counter for counting the increment of y
s = 0 #s is the counter for counting the decrement of q
z = y
while y:
y = y // 10
p += 1
q = (10 **(p-1))
while z:
print(z //q, end =", ")
z = z % q
q = q //10
s = s-1