Chemistry, asked by Anonymous, 10 months ago

Program to calculate the sums of even and odd integers of the first n natural numbers.​

Answers

Answered by Anonymous
98

\huge{\overline{\underline{\bf{\mid{Python}\mid}}}}

\huge{\underline{\tt{Codings:-}}}

n= int(input("Up to which Natural number?"))

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)

Answered by jolly4941
5

Answer:

Program to Calculate Sum of Even and Odd Numbers from 1 to N without If Statement. This Python sum of even and odd numbers program is the same as the above. However, this program allows users to enter Minimum and maximum value. Next, it prints even and odd numbers between Minimum and maximum value.

If n is even, you are calculating the sum of the first n/2 odd and n/2 even numbers. If n is odd, you are calculating the sum of the first (n+1)/2 odd and the first (n-1)/2 even numbers. Let o=floor((n+1)/2).

Similar questions