In this tutorial you will learn about the C Program to Get Input from User and its application with practical example.
C Program to Get Input from User
In this tutorial, we will learn to create a C program that will Get Input from Users in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
Get Input from the Users
As we all know the program is a collection of different data types. In c programming, we can take the input from the user in various data types. In today’s program, we will take input in many formats. The second will display the data.
With the help of this program, we can Get Input from User
Algorithm 1:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input from the user in integer format. 3. Taking the input from the user in float format. 4. Taking the input from the user in double format. 5. Printing the data taken in input from the user. 6. End program. |
Algorithm 2:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input from the user in character format. 3. Printing the data taken in input from the user in character format. 4. Printing the data taken in input from the user in ASCII format 5. End program. |
Program to Get Input from User:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> int main() { //declaring the variable for the program int no1; float no2; double no3; //taking input in integer number printf("Enter a integer number: "); //Scanning the input by the user scanf("%d", &no1); //taking input in float number printf("Enter a float number: "); //Scanning the input by the user scanf("%f", &no2); //taking input in double number printf("Enter a double number: "); //Scanning the input by the user scanf("%lf", &no3); //Printing the output integer value printf("no1 = %d\n", no1); //Printing the output float value printf("no2 = %0.2f\n", no2); //Printing the output double value printf("no3 = %lf", no3); return 0; } |
Program 2:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { //declaring the variable char chr; printf("Enter a character: "); scanf("%c", &chr); // When %c is used, a character is displayed printf("You entered %c.\n",chr); // When %d is used, ASCII value is displayed printf("ASCII value is %d.", chr); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- no1 = it will hold the integer value.
- no2 = it will hold the float value.
- no3 = it will hod the double value.
- chr = it will hold the character value.
Taking Input from the user.
Printing the data in the output.