Skip to main content

Write a C Program That Requests an Integer Value from the User. (Code and Explanation)

Write a C program that requests an integer value from the user. If the value is between 1 and 100, print “OK;” otherwise, print “Out of range”.


(explanation for code at the bottom of the post)



If you run it in Visual Studio, you will get this screen:



If you enter any number between 1 and 100 (1 and 100 included), you will get something like this:


( I entered 24)


However, if you enter a letter or any number outside the defined ranger (1-100), you will get this screen accompanied by sound alert:




Ok! let's break the code down line by line:


int userInput;

We defined an integer variable named userInput, this is where the value entered by user will be stored. int stands for integer.

--------------------------------------------------------------------------

printf("Enter an integer value between 1 and 100: ");
scanf_s("%d" , &userInput);



We wrote a printf line to display a prompt to the user asking him to enter an integer value between 1 and 100.

We wrote the scanf_s line to obtain the number from the user to store it in userInput.

--------------------------------------------------------------------------


if (userInput <= 100 && userInput >= 1)
{
printf("Ok!\n");
}
else if (userInput > 100 || userInput <1)
{
printf("Error! Out of Range...");
}



We wrote the if - else if syntax to compare the value entered by user with the acceptable range. If the user input anything from 1 to 100, the program will display the following message: "Ok!"

However, if the user enters any number outside of the acceptable range (1-100) or any letter, the program will display the following message: "Error! Out of Range..."


Popular posts from this blog

LTspice Tutorial: How to Design and Simulate a Circuit in LTspice

 If you do not have LTSpice already installed on your machine, please head over to LTSpice's page on Analog Device's website (Link: LTspice Information Center | Analog Devices ). Note: a video version of this tutorial is available here  Click on the applicable download button that suits your machine's operating system. Upon successfully downloading and launching LTspice, you will be greeted with a window that looks something like in the figure below. Alright, now we will need to create a new schematic. That can be done by either of the following approaches:  click on File then choose New Schematic . click on the New Schematic symbol   on the toolbar. on your keyboard, press Ctrl+N . Upon doing so, a new grey-ish screen will pop up... that will be your new empty schematic (just like the one in the figure below). In this example, we will try to design and simulate a very basic voltage divider.  To recap: a voltage divider is a circuit that takes a certain i...

LTspice Tutorial: How to Design and Simulate a Circuit in LTspice (Video Version)

Text version with screenshots can be found by clicking here If you do not have LTSpice already installed on your machine, please head over to LTSpice's page on Analog Device's website (Link:  LTspice Information Center | Analog Devices ). Click on the applicable download button that suits your machine's operating system.

How Does an Inverter (NOT) Gate Work - NOT Gate Explained

For a video version of this tutorial, please refer to this link . An inverter gate, or NOR gate for short, is one of the essential logic gates used constructed digital circuits. As implied by its name, the NOT gate works by taking an input, inverting it, then outputting the inverted value. The NOT gate schematic symbol looks like a triangle with clown nose on the Output side (as shown in the Figure above). In logic circuits, inputs and outputs are binary, which means their value can either be 0 or 1. So: If NOT receives a 0 at the input, it will output 1 . If NOT receives 1 at the input, it will output 0 . To sum it up, the truth table for the NOT gate will be as the following: Input Output X F 0 1 1 0 The NOT gate can be algebraically represented as the following:  F = X' (or you can have a bar above the X) where :     F is the output and X is the input