Write a program that requests integer numbers from the user repetitively though
ID: 670681 • Letter: W
Question
Write a program that requests integer numbers from the user repetitively though the keyboard (using printf and scanf) until the user enters 0. Createtwo variables that accumulate the numbers entered, one for the even numbers and the otherthe odd numbers. The program should also ask the name of the user and save it as a string.Upon completion of the entering process, the program prints out the name of the user, thenumber and the total of the even numbers entered, and the number and the total of the odd numbers entered
So far I have:
#include
#include
int main ()
{
int n, integer, sumOdd=0, sumEven=0;
//*Asks users name*//
char name [100];
printf("What is your name? ");
scanf("%s", &name);
printf("Enter any integer: ");
scanf("%d", &integer);
for (integer > 0, integer == 0, integer++)
if(integer %2 = 0){
sumEven = sumEven + integer;
}
if(integer %2 == 0) {
sumOdd = sumOdd + integer;
}
printf("%s, the numbers you entered are broken down as follows: You entered even numbers with a total value of . You also entered odd numbers with a total value of ",name);
system("pause");
return 0;
*Basically I'm incredibly stuck and I think I'm just overthinking what it's asking*
Explanation / Answer
Oh wow. You are very good with your logic. You did just some minute mistakes. The loop you wrote is syntactically and also semantically wrong. Its a good to use a while loop instead of for. Usually, you use a for loop, when you are so clear on howmany times you are going to run the loop. We are not clear on that here. As long as the user didn't entered a 0, you want the loop to be repeated. Ofcourse, its a good practice to use a while in this case, but not a rule.
After correcting all the other minute mistakes, the final code goes here:
#include <stdio.h>
int main ()
{
int n, integer, sumOdd=0, sumEven=0;
/*Asks users name*/
char name [100];
printf("What is your name?: ");
scanf("%s", name);
printf("Enter any integer: ");
scanf("%d", &integer);
while(integer != 0)
{
if(integer %2 == 0)
{
sumEven = sumEven + integer;
}
if(integer %2 != 0) /*You need not perform this check, you can simply write an else.*/
{
sumOdd = sumOdd + integer;
}
printf("Enter any integer(0 to exit.): "); /*Here you need to read the next integer*/
scanf("%d",&integer);
}
printf("%s, the numbers you entered are broken down as follows: You entered even numbers with a total value of %i. You also entered odd numbers with a total value of %i ",name,sumEven, sumOdd);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.