21 Nov C – User Input
To get user input in C language, use the scanf(). It is used to read the data input by the user on the console. Let us see some example C programs to get input from the user.
C program to print an integer entered by the user
Let us see an example to enter an integer from the user using scanf() and display using printf():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { int i; // Asks the user to enter a number printf("Enter a number = "); scanf("%d",&i); // Displays the number entered by the user above printf("\nThe number entered by user = %d ",i); return 0; } |
Output
1 2 3 4 |
Enter a number = 10 The number entered by user = 10 |
C program to print the multiplication of integers entered by the user
Let us see another example wherein we will get three numbers from the user and multiply them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main() { int a, b, c; // Three numbers int res; // Asks the user to enter three numbers one-by-one printf("Enter the 1st number = "); scanf("%d",&a); printf("Enter the 2nd number = "); scanf("%d",&b); printf("Enter the 3rd number = "); scanf("%d",&c); // Multiply the numbers entered by the user res = a*b*c; printf("Multiplication Result = %d ",res); return 0; } |
Output
1 2 3 4 5 6 |
Enter the 1st number = 5 Enter the 2nd number = 10 Enter the 3rd number = 15 Multiplication Result = 750 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments