Output: \"Please enter a positive integer: -3 I\'m sorry, you must enter a posit
ID: 670116 • Letter: O
Question
Output:
"Please enter a positive integer: -3
I'm sorry, you must enter a positive integer greater than zer: why?
I'm sorry, you must enter a positive integer greater than zero:-42
I'm sorry, you must enter a positive integer greater than zero: 42
The positive interger was: 42"
I'm supposed to write a program that is supposed to ask for a positive input and if something other than a positive integer is put into the system, it'll keep prompting the user for a positive number. Also attempt to clear the keyboard buffer by reading characters from the keyboard up to and including a new line.
#include<stdio.h>
void loop_trap
int main()
{
int k = 1;
do
{
printf("Please enter a positive integer: ");
scanf("%d, &k");
}
while (k > 0);
return 0;
}
if (i <= 0);
{
printf("I'm sorry, you must enter a positivie integer greater than zero:")
}
Explanation / Answer
#include<stdio.h>
int main()
{
int k;
printf("Please enter a positive integer: ");
scanf("%d", &k);
while (1)
{
if(k>0)
{
printf("The positive integer was: %d ",k);
break;
}
printf("I'm sorry, you must enter a positive integer greater than zero: ");
scanf("%d", &k);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.