Print a Number and Store Value in Variable in C++

In todays article, we are going to use a variable. Variable is just like a space where we are going to store our number and then print it on console or screen. Remember our “Hello World!” program. In that program, we didn’t store any value. Instead of storing, we just print a static line on console using cout. In this program, we will store a value in a variable and then print this value. So this program will be dynamic. You can store any number and print it.

Code of Print a Number Program in C++

#include <iostream>
using namespace std;

int main()
{    
    int number=0;

    cout << "Please enter a number: ";
    cin >> number;

    cout << "Your number is " << number;    
    return 0;
}

Input and Output

input: 12
output: Your number is 12

Structure of C++ Program

We have talked about #include, int main(), cout, and return 0; in our last article. Are you still confused? You can go to Hello World! Program and learn about these things.

Variables in C++

As we talked about earlier about variables, variables are just an allocated space in our program to store any value. In C++, there are many type of variables and these are int, float, string, boolean, long, and many others. Today, we are going to talk about int variable in c++. int stands for integer value. In int type of variable, we can store any number without float points. We can store 7, 12,662 or any type of number, but we can’t store any float numbers, which are 7.77 or 122.332. This type of variable will ignore the numbers after float point and store the starting value. So when we are sure about integer, we will use int variable just like we are doing in this program.

Garbage Values in C++

Maybe you have noticed that i just wrote a line in code int number=0;. We can do something like int number; . We can declare any variable with equaling it against any number. But the reason of choosing equaling it in while declaring is C++ will store any random number in it. That random number is called garbage value and we have to avoid it. These long number will make the size of our program large. That why we equaling it against zero when we declare the variable.

cin >> in C++

We have studied about cout << in last article but what is cin? Well, this is the way we will use to take input of number. By using cin >>, we will store the value in our variable and later we will display this value on console or screen. So every time, when we will need to take input from user, we will use cin >>.

cout with variable

You might be noticed a slight difference in cout here. We are printing our variable without quotes. This is how C++ will get to know that this is a variable. Everything without quote will be detected as variable in c++. When the compiler will come to this cout line, it will sense that there is a variable in this cout statement. After that, compiler will go to variable and takeout the value from it and print that value on screen. How cool it is.

Conclusion

Today we have learnt about Variables, printing a variable, taking input in c++ with cin>>, and garbage values. If you are still confused in any point, you can just comment down. You can download dev C++ from here.

Leave a Comment