C Program to get and display a number
C Program to get and display a number
In this tutorial, we will practice C Program. A C program to get and display a number to the user. The program will prompt the user to enter a number ( for example the user’s age ). The program will store the number into a variable and will print the entered age onto the screen.
C Program
/** ********************************** * Program Description: * C Program to Get and Display a Number * Filename: display.c * C Tutorials - www.TestingDocs.com ************************************* */ #include int main() { // declare int variable int age; printf("Please enter your age ::"); scanf("%d",&age); printf("Your age is %d years.\n",age); return 0; } // end main
Sample Output
Please enter your age :: 27
Your age is 27 years.
Process returned 0 (0x0) execution time : 12.541 s
Press any key to continue.
Explanation
The printf() statement prompts the user to enter the number i.e the age
The scanf() statement accepts the input from the standard input device(the keyboard). %d is the format specifier that denotes that the integer data type( int) is expected as the input. The %d in the printf() statement substitutes the value of the integer variable and displays the formatted output.