Enter your email address:


Monday, September 2, 2013

Home » , » Write a C program which asks the user for a number between 1 to 9 and shows the number. If the user inputs a number out of the specified range, the program should show an error and prompt the user for a valid input.

Write a C program which asks the user for a number between 1 to 9 and shows the number. If the user inputs a number out of the specified range, the program should show an error and prompt the user for a valid input.


Questions: Top-30-c-programs-asked-in-interview_6304.html

19.Write a C program which asks the user for a number between 1 to 9 and shows the number. If the user inputs a number out of the specified range, the program should show an error and prompt
the user for a valid input.
Program: Program for accepting a number in a given range.
#include
int getnumber( );
int main(){
int input = 0;
//call a function to input number from key board
input = getnumber( );
//when input is not in the range of 1 to 9,print error message
while (!((input = 1))){
printf("[E RROR] The number you entered is out of range");
//input another number
input = getnumber( );
}
//this function is repeated until a valid input is given by user.
printf("\n The number you entered is %d", input);
return 0;
}/
/this function returns the number given by user
int getnumber( ){
int number;
//asks user for a input in given range
printf("\n Enter a number between 1 to 9 \n");
scanf("%d" ,&number);
return (number);
}
Output:
Enter a number between 1 to 9
45
[ERROR] The number you entered is out of range
Enter a number between 1 to 9
4
The number you entered is 4
Explanatio n:
getfunctio n() function accepts input from user.'while'loop checks whether the number falls within range or not
and accordingl y either prints the number(If the number falls in desired range) or shows error message(nu mber is
out of range).
Share this games :

0 comments:

Post a Comment

Related Posts