Write a C Program to Print an Integer Entered By the User.
In this Article, you will learn how to print an integer entered by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int number;
// Prompt the user to enter an integer
printf("Enter an integer: ");
// Read the integer input from the user and store it in 'number'
scanf("%d", &number);
// Display the entered integer with a description
printf("You entered: %d\n", number);
}
This program uses a integer variable number to store the user's input. It prompts the user to enter their name using printf and reads the user's input using scanf. The "%d" format specifier tells scanf to read an integer from the user and store it in the variable number.
After successfully reading the input, the program displays the entered integer back to the user using printf. The message "You entered: " is shown, followed by the value of the variable number.
When you run this program, it will prompt you to enter an integer. Once you provide an input, it will display the integer you entered. For example:
Output:
Please note that this program assumes the user will enter a valid integer value. If the user enters something other than an integer, unexpected behavior may occur. In practice, it's a good idea to add error handling and input validation to ensure the program behaves as expected even with incorrect inputs.