Ask Question

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, on a single line and separated by a single space, the sum of all the even integers read and the sum of all the odd integers read. python

+3
Answers (1)
  1. 11 July, 16:06
    0
    The program to this question as follows:

    Program:

    #defining variable and assign value

    odd=0

    even=0

    i=1

    print ('Input 0 after inserting all number: ') #print message

    while i>0: #define loop for check inserting and add all number

    i=int (input ()) #input value by user end

    if (i< 0) : #check value is not positive

    break

    if ((i % 2) = =0and (i>0)) : #check even number condition

    even=even+i #add even numbers

    if ((i % 2) !=0and (i>0)) : #check odd number condition

    odd=odd+i #add odd numbers

    print ('Sum of Even number:', even,'and','Sum of Odd number:', odd) #print value

    Output:

    Input 0 after inserting all number:

    1

    2

    3

    5

    7

    0

    Sum of Even number: 2 and Sum of Odd number: 16

    Explanation:

    In the above python program, three variable is defined, that is "even, odd and i", in which "even and odd" variable assign value that is "0", and the variable i is used for inserting elements from user. In the next line a while loop is defined that inserts numbers in variable "i", in this loop, 3 if block is used that can be defined as follows:

    In first if block variable "i", checks that value is non-positive number if this condition is true, it will break the loop. In second if block this block check even number condition and add in even variable. In third, if block this block check odd number condition and add in odd variable.

    End of the loop we use print function that prints the sum of even numbers and odd numbers.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers