-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsing for and while loops.py
124 lines (82 loc) · 4.17 KB
/
Using for and while loops.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'''Programs to perform specific functions and display output in a particular format using for and while loops only'''
##1.a)To display the numbers divisible by 3 or 5, 5 in each line and print their sum and count
##1.b)To print the sum and count of odd multiples of 11 in a specified range taken as input
##2.To display specific patterns of the numbers and the alphabet based on choice input
##3.To generate bus charge fares based on distance travelled and the given tariff
while True:
print("You can choose to view the following programs:")
print("A : You can see a format of the numbers in the range 20 and 100 which are divisble by 3 and 5 along wit their count and sum!")
print("B : If you input a certain range, you can see the sum and count of all the odd multiples of 11 withing that range!")
print("Exit: You may exit the program!")
print()
choice = input("Enter your choice(a/b/Exit): ")
if choice == "a" or choice == "A":
sum = count = 0 #Initializing
for i in range(20,101):
if (i%3 == 0) or (i%5 == 0):
print(i,end=" ")
sum += i
count += 1 #Incrementing the count
if count%5 == 0:
print("\n")
print("\n\nThe sum of the numbers divisible by 3 and 5 is: ",sum)
print("The count of the numbers divisible by 3 and 5 is: ",count)
print()
elif choice == "b" or choice == "B":
sum = count = 0
lower_limit = int(input("\n\nEnter a lower limit: "))
upper_limit = int(input("Enter an upper limit: "))
for i in range(lower_limit,upper_limit + 1):
if (i%11 == 0) and (i%2 != 0): #Check for divisibility by 11 and 2
sum += i
count += 1
print("The number of odd multiples of 11 in the specified range is: ",count)
print("The sum of the odd multiples of 11 is: ",sum)
print()
elif choice == "Exit" or choice == "exit" or choice == "EXIT":
print()
print("The program will end now!")
print("Thank you!")
break
else:
print("Invalid choice!Please choose again!\n")
continue
"""OUTPUT:
You can choose to view the following programs:
A : You can see a format of the numbers in the range 20 and 100 which are divisble by 3 and 5 along wit their count and sum!
B : If you input a certain range, you can see the sum and count of all the odd multiples of 11 withing that range!
Exit: You may exit the program!
Enter your choice(a/b/Exit): A
20 21 24 25 27
30 33 35 36 39
40 42 45 48 50
51 54 55 57 60
63 65 66 69 70
72 75 78 80 81
84 85 87 90 93
95 96 99 100
The sum of the particular numbers is: 2340
The count of the particular numbers is: 39
You can choose to view the following programs:
A : You can see a format of the numbers in the range 20 and 100 which are divisble by 3 and 5 along wit their count and sum!
B : If you input a certain range, you can see the sum and count of all the odd multiples of 11 withing that range!
Exit: You may exit the program!
Enter your choice(a/b/Exit): B
Enter a lower limit: 100
Enter an upper limit: 220
The number of odd multiples of 11 in the specified range is: 5
The sum of the odd multiples of 11 is: 825
You can choose to view the following programs:
A : You can see a format of the numbers in the range 20 and 100 which are divisble by 3 and 5 along wit their count and sum!
B : If you input a certain range, you can see the sum and count of all the odd multiples of 11 withing that range!
Exit: You may exit the program!
Enter your choice(a/b/Exit): C
Invalid choice!Please choose again!
You can choose to view the following programs:
A : You can see a format of the numbers in the range 20 and 100 which are divisble by 3 and 5 along wit their count and sum!
B : If you input a certain range, you can see the sum and count of all the odd multiples of 11 withing that range!
Exit: You may exit the program!
Enter your choice(a/b/Exit): EXIT
The program will end now!
Thank you!
"""