Write a loop that reads positive integers from standard input and that terminate
ID: 3687303 • Letter: W
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 the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.
--------------------------------------------------------------
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 the sum of all the even integers read, the sum of all the odd integers read, a count of the number of even integers read, and a count of the number of odd integers read, all separated by exactly one space. Declare any variables that are needed.
Explanation / Answer
#include <stdio.h>
int main()
{
int num,digit,evnsum ,oddsum;
printf("Enter integer number: ");
scanf("%d",&num);
while (num> 0)
{
digit = num % 10; /* separate LS digit from number */
if (digit % 2 == 1)
{
oddsum=oddsum+digit;
}
else
{
evnsum= evnsum+digit;
}
num /= 10;
}
printf("Sum Of Even and Odd : %d %d ", evnsum, oddsum);
return 0;
}
OutPut:
Enter integer number: 345678-1 Sum Of Even and Odd : 18 15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.