C++ Program to Print Number Entered by User

by | Jan 1, 2023 | C++ Programs

Introduction

In this program, you will learn to print a number on the screen. The user is asked to enter a number, and that will get stored in a variable that needs to be declared earlier.

Although the code looks quite easy and simple, it’s important to start the journey of programming with basic concepts.

To understand this program, you should know about the following concepts of C++.

1. Variables, Constants, and literals

2. Data types

3. Input-output statements

Program

#include<iostream>
using namespace std;
int main()
{
float num;
cout<<"Enter a number:\n";
cin>>num; // taking input from user
cout<<"The value entered is :"<<num<<endl; // displaying output
return 0;


}

 

Output

Explanation

The program starts with the basic statements that are

#include<iostream>

using namespace std;

After that, we declare the main function with the ‘int’ return type.

float num;

‘num’ is a variable declared with ‘float’ data type to store ‘float’ type value.

cout<<“Enter a number:\n”;

The program asks the user to enter a number. It is done with cout statement and whatever written inside “” gets printed as it is.

\n is used to print a new line.

cin>>num;

The value entered by the user gets stored in the ‘num’ variable using cin.

cout<<“The value entered is :”<<num<<endl;

Again, using the cout statement the ‘num’ value is displayed on the screen. At the place of ‘num’ its value gets printed and the endl will change the line.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Advertisement

Advertisement

Advertisement

Advertisement