Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22871

IslandT: Python Example — Count the number of even numbers from 1 to 100

$
0
0

An even number is a number that can be divided by two, for example, 2, 4, 6, 8, and 10. In this example let us create a simple Python program that can count the number of the even numbers from 1 to 100!

I am going to use the modulus operator to find the numbers of the even numbers from 1 to 100. I am also going to use the range method and the for loop to loop from 1 to 100.

even = 0
    for count in range(1,101): # not include 101
        if count % 2 == 0:
            even+=1
    print(even) # 50

How about if you want to find the number of odd numbers within that range? Simple just used count % 2 != 0.


Viewing all articles
Browse latest Browse all 22871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>