Home » Program to calculate and print the sums of even and odd integers of the first n natural numbers in Python

Program to calculate and print the sums of even and odd integers of the first n natural numbers in Python

Program to calculate sums of even and odd integers of first n natural numbers in python

n=int(input("Up to which natural number do you want to add? :"))
ctr=1
sum_even=sum_odd=0
while ctr<=n:
    if ctr%2==0:
        sum_even+=ctr
    else:
        sum_odd+=ctr
    ctr+=1
print("The sum of even integers is",sum_even)
print("The sum of odd integers is",sum_odd)

Contents

Output

Up to which natural number do you want to add? :8

The sum of even integers is 20

The sum of odd integers is 16