Sunday 22 September 2024

Practical - 74 - Write a Python program to receive numbers from user through keyboard until user inputs 0 to end the input process, then the program calculates and displays the sum of given odd numbers and even numbers respectively.

Practical - 74 - Write a Python program to receive numbers from user through keyboard until user inputs 0 to end the input process, then the program calculates and displays the sum of given odd numbers and even numbers respectively.


Ans:


sum_odd = 0

sum_even = 0

while True:

    num = int(input("Enter a number (0 to stop): "))

    if num == 0:

        break

    if num % 2 == 0:

        sum_even += num

    else:

        sum_odd += num

print("Sum of odd numbers: ",sum_odd)

print("Sum of even numbers: ",sum_even)

 

Output:

Enter a number (0 to stop): 3

Enter a number (0 to stop): 7

Enter a number (0 to stop): 8

Enter a number (0 to stop): 9

Enter a number (0 to stop): 6

Enter a number (0 to stop): 4

Enter a number (0 to stop): 0

Sum of odd numbers:  19

Sum of even numbers:  18


No comments:

Post a Comment