![]() |
VOOZH | about |
Given an input value N, the task is to allow taking only integer input from the user.
Now, if the user enters any input other than an integer, that is, a character or symbol, it will not be accepted by the program. Below is the C program to implement this approach:
Output:
Explanation: In the above program, if the input is a character value, then the scanf() function will take the input but will not print anything on the output screen or it will ignore the character and print the integers only. So, to avoid this problem, let's discuss how to write a program that takes only integer input only. Below are the steps for the same:
Steps: The steps involved in the program below are:
do
{
If( ch>=48 && ch<=57)
}
Here ch is the input to be enter and 48 to 57 is the
ASCII Code of 0 to 9 respectively. num = num * 10 + (ch - 48) Here num * 10 will change the place value of the integer to make it digit and (ch - 48) will add the integer by subtracting its ASCII code.
if(ch == 13) break; Here 13 is the ASCII code of carriage return which breaks and return the input value.
printf("you have entered %d", x)
Below is the C program to implement the above approach:
Output:
Explanation: It can be observed while executing the program, that the program is only accepting the integer input and printing the input on the screen.