C++ Program To Add Two Numbers
C++ is a powerful language and it offers many operations to be performed on numbers. In this tutorial, I will show you that how you can make a C++ program to add two numbers using ‘+‘ operator.
Program Logic & Structure:
In this program, we will declare three variables. Two of these would be used to collect input from user and third variable will contain their sum. But before proceeding, I want to tell you that variables are memory locations where a value can be stored and retrieved. Each variable in C++ has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. The name of a variable can be composed of letters, digits, and the underscore character. In this case, we will use integer data type as we need numeric data from user. We will use cin statement to collect input from user. After collecting input, we will store the sum in third variable. To compute the sum, We will use ‘+‘ operator. This operator is used to add values of two variables. After that, we will use cout statement to print sum on the screen.
As program starts, it asks the user to input first and second number. After collecting input from user, it calculates the sum and stores this value in third variable. After that, it prints the sum on screen.
Code To Add Two Numbers in C++:
Here is the code of program. you will have to open your Dev C++ IDE and write this code in editor.
using namespace std;
cout<<"ENTER FIRST NUMBER: ";
cout<<"ENETR SECOND NUMBER: ";
cout<<"THE SUM IS: "<<total;
Just press F11 to compile and execute this program. Here is the output of program:
How Program Works:
Below is the basic structure of a C++ program. No matter, how complicated or simple a program is, every C++ program have these lines.
using namespace std;
I have explained these lines fully in my previous post. If you are curious about the purpose of these lines then read my article: Introduction of C++ Language & C++ Hello World Program. I will explain rest of the lines in detail here:
In this line, we declared three variables. These variables are used to store numbers. At the beginnng of this line, the keyword ‘int‘ is used to declare variables as integer type. n1,n2 and ‘total‘ are the names of variables. Comma ( , ) is used to separate variable names and shows that these are three distinct variables. Here is the table of variables used:
|n1||Integer||This variable is used to store first number entered by user.|
|n2||Integer||This variable is used to store second number entered by user.|
|total||Integer||This variable is used to store sum of two numbers.|
Remember that this program calculates sum only for whole number means it does not consider the digits coming after decimal point. If you want this program to work for real numbers then use keyword ‘float‘ instead of ‘int‘. If you want complete program then Download Exercise Files.
cout statement is used to print something on screen. By using this statement, we prompted the user to input first number. cout statement prints the text which is written in double quotations ( ” ” ) on the screen.
cin statement is used to collect input from user. ‘cin‘ is a keyword and symbol( >> ) is called extraction Operator. This statement reads the keys pressed at keyboard and stores values in a variable. In our case, this statement reads the number typed by user and stores it into ‘n1‘ variable.
Again cout statement is used to prompt the user to input value for second variable. This statement prints “ENTER SECOND NUMBER: ” on screen without double quotations.
Here we used cin statement again to collect input from user. This time it reads the number typed by user and stores values in ‘n2‘ variable.
This statement is an arithmetic expression which assigns the value of the expression n1 + n2 (the sum of the integer values stored at n1 and n2) to the variable ‘total‘. We used ‘+’ operator to sum the values of n1 and n2 variables. Lastly, there is an assignment operator ( = ) which assigns the value of n1 + n2 to variable ‘total‘.
This line makes use of cout statement to show output on screen. It print the text enclosed in double quotations as it is and then shows the value of variable ‘total‘. It is strongly recommended that do not enclose ‘total‘ in double quotations.
This was all about C++ program to add two numbers.
Rest of the lines have been explained in my article: Introduction of C++ Language & C++ Hello World Program.
Thanks for reading this article. If you have any question then you can ask me in comments.
You can also Click Here to Download Exercise Files.