Using looping with range() and conditional statement. create a program that would diplay all the numbers that are divisible by 2 from 1 to 10. If the number is divisible by 2 the program would print "2 is divisible by two" otherwise if the number is not divisble by two the program would print "3 is not divisible by two". Sample Program Output: 1 is not divisible by two 2 is divisible by two 3 is not divisible by two 4 is divisible by two 5 is not divisible by two 6 is divisible by two 7 is not divisible by two 8 is divisible by two 9 is not divisible by two 10 is divisible by two
Answers
Answered by
7
The following cσdes have been written using Python.
for i in range(1, 10):
if i%2 == 0:
print(i, "is divisible by two.")
else:
print(i, "is NOT divisible by two.")
We've given the range values as (1, 10), meaning that 'i' will traverse from 1 to 10. As it traverses it tests for the if statements. If the first test expression results to True, i.e., if 'i' is divisible by 2, it will proceed to the statement and if 'i' isn't divisible by 2, it'll move to the else statement.
A for loop is an iteration statement used for traversing through elements/for repeated checking. It generally follows a similar syntax:
for i in <traversing element>:
statement
Similar questions