1 Write a validation loop that prompts the user for a number
1.) Write a validation loop that prompts the user for a number between 100 and 200, inclusive. If the number is outside that range, print a message and prompt the user again.
2.) Write a sentinel loop that prompts the user for a character between ‘d’ and ‘s’, inclusive. Whether the character is inside or outside that range, print a message. Continue to prompt the user for a character until they enter ‘!’ (the sentinel value).
Solution
1)
#include <stdio.h>
int main(void)
{
int number;
do
{
printf(\"\ Enter the number in the range 100 and 200 inclusive\");
scanf(\"%d\",&number);
if(number >= 100 && number <= 200)
break;
}while(number <100 || number > 200);
return 0;
}
output:
Success time: 0 memory: 2172 signal:0
2)
#include <stdio.h>
int main(void)
{
char ch;
do
{
printf(\"\ Enter a character between \'d\' and \'s\' inclusive\");
scanf(\"%c\",&ch);
if(ch >= \'d\' && ch <= \'s\')
break;
}while(ch <\'d\' || ch > \'s\');
return 0;
}
output:
Success time: 0 memory: 2172 signal:0
