This program takes two numbers, add them & displays the result to the user. This is a very simple program to create, but sometimes as a beginner we might feel overwhelmed while learning a language so, here we are to help you.
Table of Contents
Source Code
To add two numbers or any amount of numbers there are basically two ways. We will discuss both of them, with source code, explanation & it’s output.
Now, here are those two ways –
- Adding 2 Numbers By Letting The User Input The Values
- Adding 2 Numbers By Defining The Values Inside The Code
Lets discuss each of them individually.
Adding 2 Numbers By Letting The User Input The Values
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << “Enter two numbers\n”;
cin >> a >> b;
c = a + b;
cout <<“Sum = ” << c << endl;
return 0;
}
Explanation
This code lets the user decide the values to be added together. Sometimes, we need to let the user decide the values depending upon the type of C++ program we are creating. In some situations, that is the best solution we might be able to offer.
How Does This Code Works?
When compiled and executed it prints a line on the screen saying “Enter two numbers” by using “cout“. After that we need some place or variable to store those two values into.
So, we created two variables called “a” & “b”. Now after that we also need another variable to store the result into. We created another variable called “c”. Now, after that we use “cin” to store those two numbers into variable “a” & variable “b”.
When these values are stored we tell that result which is c = a + b. Which is a way of saying that my result is the sum of the variable “a” & variable “b”.
At last, we print the result which is stored in variable “c” by using “cout“.
Output
Adding 2 Numbers By Defining The Values Inside The Code
#include <iostream>
using namespace std;
int main() {
int a, b, c; a=5; b=4;
cout << “The Sum Is\n”;
c = a + b;
cout <<“Sum = ” << c << endl;
return 0;
}
Explanation
This code lets the user decide the values to be added together. Here, we have already defined the values to be added so now we don’t need to let the user decide the values, depending upon the type of C++ program we are creating. In some situations, already defining the values is what we want.
How Does This Code Works?
When compiled and executed it prints a line on the screen saying “The Sum Is” by using “cout“. Then we define 3 variables just like before. One variable to store the first number, one variable to store another & another one to store the result into.
Now, after defining the values we define the result which is going to be stored in variable “c”. We tell the compiler that c = a +b.
At last, we print the result which is stored in variable “c” by using the “cout” keyword.
Conclusion
Well, congratulations! now you know both methods to add numbers in C++. Depending upon the type of your requirement you may use one or the other method often. But, at least you know how to make a C++ program that does so.