In: Computer Science
Python Language:
Similar to Project 3, write a program that loops a number from 1 to 10 thousand and keeps updating a count variable according to these rules:
if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above conditions match for the number, increase count by the number.
Before the loop begins, count should be set to 0. Once the loop is complete, print the value of count.
n1, n2, and n3 should be set as follows:
n1 = 11
n2 = 64
n3 = 107
Just like the previous projects, the value of a=1, the value of b=2, ..., the value of z=26.
The program must produce one numeric output (it should be the value of count).
Python code:
#setting count as 0
count=0
#setting n1 as 11
n1=11
#setting n2 as 64
n2=64
#setting n3 as 107
n3=107
#looping from 1 to 10000
for i in range(1,10001):
#checking if number is divisible by n1
if(i%n1==0):
#incrementing count by 1
count+=1
#checking if number is divisible by n2
elif(i%n2==0):
#incrementing count by 2
count+=2
#checking if number is divisible by n3
elif(i%n3==0):
#incrementing count by 3
count+=3
else:
#incrementing count by number
count+=i
#printing count
print(count)
Screenshot:
Output: