Write a function definition for a function named getValidAge that returns an integer to the calling program. The number returned by the function is a valid age (between 0 and 130 inclusive). Inside the function definition, the user is asked to enter their age, the age is validated using a loop, and the valid age is returned.
//C program
#include <stdio.h>
//function to validate age
int getValidAge()
{
int age;
//while loop runs until user enters the age between 0 to 130 inclusive
while(1)
{
printf("\n\nEnter your age:\n");//user input for age
scanf("%d",&age);
//condition to check the age in between 0 to 130 inclusive otherwise invalid age
if(age>=0 && age<=130)
{
return age;
break;
}
else
{
printf("Entered invalid age");
continue;
}
}
}
int main(void)
{
int result;
result = getValidAge();//function call
printf("Entered age is : %d ",result); //prints the result
return 0;
}
Output::
Note:: Need any help regarding above program please comment.
Get Answers For Free
Most questions answered within 1 hours.