Computer Science, asked by xxxcccccc12, 5 months ago

write a program in python to calculate and print the sum of even and odd integers of the first n natural number​

Answers

Answered by harshitranjan321
15

Answer:

#Python program to calculate sum of odd and even numbers using for loop

max=int(input("please enter the maximum value: "))

even_Sum=0

odd_Sum=0

for num in range(1,max+1):

   if (num%2==0):

       even_Sum=even_Sum+num

   else:

       odd_Sum=odd_Sum+num

print("The sum of Even numbers 1 to {0} = {1}".format(num,even_Sum))

print("The sum of odd numbers 1 to {0} = {1}".format(num,odd_Sum))

case 1

please enter the maximum value: 10

The sum of Even numbers 1 to 10 = 30

The sum of odd numbers 1 to 10 = 25

case 2

please enter the maximum value: 100

The sum of Even numbers 1 to 100 = 2550

The sum of odd numbers 1 to 100 = 2500

 

Calculate the sum of odd and even numbers using while loop

Program 2

This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from the from 1 to entered digits using a while loop.

#Python program to calculate sum of odd and even numbers using while loop

max=int(input("please enter the maximum value: "))

even_Sum=0

odd_Sum=0

num=1

while (num<=max):

   if (num%2==0):

       even_Sum=even_Sum+num

   else:

       odd_Sum=odd_Sum+num

   num+=1

print("The sum of Even numbers 1 to entered number", even_Sum))

print("The sum of Even numbers 1 to entered number", odd_Sum))

Explanation:

please mark as brainliest

Answered by hiidkmyname
6

Explanation:

Num=int(input("Enter a value: "))

sumeven=0

sumodd=0

for ctr in range(1,Num+1):

   if (ctr%2==0):

       sumeven=sumeven+ctr

   else:

       sumodd=sumodd+ctr

print ("Sum of the even numbers is: ",sumeven)

print ("Sum of the odd numbers is: ",sumodd)  

This should work as well

Similar questions