In this program, we will show you how to write a C program that can read an integer from the user and check the specified range to which it belongs. We will also explain the logic behind the program and how to test it with some sample inputs.
C Program
The program we are going to write is as follows:
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num < 0 || num > 80)
{
printf("Error: number is out of range.\n");
}
else if (num >= 0 && num <= 20)
{
printf("Number is in the range [0, 20].\n");
}
else if (num >= 21 && num <= 40)
{
printf("Number is in the range [21, 40].\n");
}
else if (num >= 41 && num <= 60)
{
printf("Number is in the range [41, 60].\n");
}
else if (num >= 61 && num <= 80)
{
printf("Number is in the range [61, 80].\n");
}
return 0;
}
Explanation
- The logic behind this program is simple. We first include the standard input/output header file 'stdio.h' which provides us with the functions 'printf' and 'scanf'. These functions allow us to print messages on the screen and read inputs from the keyboard.
- Next, we declare an integer variable 'num' which will store the input from the user. We then use the 'printf' function to prompt the user to enter an integer. We use the 'scanf' function to read the input from the user and store it in the variable 'num'.
- After reading the input, we use a series of 'if-else' statements to check the range of the input. The 'if-else' statements are conditional statements that execute a block of code depending on whether a condition is true or false.
- The first 'if' statement checks if the input is less than zero or greater than 80. If this condition is true, it means that the input is out of range and we print an error message using the 'printf' function.
- The next 'else if' statements check if the input is within a specific range. For example, the second 'else if' statement checks if the input is greater than or equal to zero and less than or equal to 20. If this condition is true, it means that the input is in the range [0, 20] and we print a message using the 'printf' function.
- We repeat this process for each range until we cover all possible cases. The last 'else if' statement checks if the input is greater than or equal to 61 and less than or equal to 80. If this condition is true, it means that the input is in the range [61, 80] and we print a message using the 'printf' function.
- Finally, we return zero from the main function to indicate that the program has ended successfully.
Output
To test our program, we can compile it using a C compiler and run it using a terminal or an IDE. We can then enter some sample inputs and see how our program responds.
For example, if we enter 15 as our input, our program will print:
Enter an integer: 15
Number is in the range [0, 20].
If we enter -5 as our input, our program will print:
Enter an integer: -5
Error: number is out of range.
If we enter 50 as our input, our program will print:
Enter an integer: 50
Number is in the range [41, 60].
Conclusion
In this program, we have learned how to write a C program that can read an integer from the user and check the specified range to which it belongs. We have also explained the logic behind the program and how to test it with some sample inputs.
This program can be useful for beginners who want to practice their C programming skills and learn how to use conditional statements and input/output functions. We hope you found this blog post helpful and informative. If you have any questions or feedback, please feel free to leave a comment below. Thank you for reading and happy coding!
We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!