Write a loop using a sentinel value. The sentinel value will be \"999\" and when
ID: 3652021 • Letter: W
Question
Write a loop using a sentinel value. The sentinel value will be "999" and when this number is entered, theloop will end. The user will enter a number. You will examine the number and determine if it is even or
odd. If it is even, you will add one to the even counter. If it is odd, add one to the odd counter.
When the loop has ended, print out a message telling how many even and odd numbers were entered.
This is as far as i've gotten. I cant get the break statement to work or the even and odd counter. thanks for the help.
int main()
{
int i;
float num, even, odd;
i = 0;
while (i >=1);
num++;
printf("Enter a number (999 to exit): ");
scanf("%f", &num);
if (num==999){
printf("You have entered %d even numbers ",even);
printf("You have entered %d odd numbers " ,odd);
break;
}
if(num==2){
even += 1;
}
else{
odd += 1;
}
system("PAUSE");
}
Explanation / Answer
#include<stdio.h>
int main()
{
int i, even=0, odd=0;
int num;
while(1)
{
num++;
printf("Enter a number (999 to exit): ");
scanf("%d", &num);
if (num==999){
break;
}
if(num%2==0){
even += 1;
}
else{
odd += 1;
}
}
printf("You have entered %d even numbers ",even);
printf("You have entered %d odd numbers " ,odd);
system("PAUSE");
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.