In this post, we will learn how to add three numbers using C++ Programming language.
This program asks the user to enter three integers, then it computes the sum of these three numbers using simple arithmetic operators.
So, without further ado, let’s begin this tutorial.
C++ Program to Add Three Numbers
// C++ Program to Add Three Numbers #include <iostream> using namespace std; int main(){ int a, b, c, sum; // Asking for input cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; // Calculating sum sum = a + b + c; // Displaying output cout << "Sum of three numbers: " << sum << endl; return 0; }
Output
Enter first number: 10
Enter second number: 15
Enter third number: 20
Sum of three numbers: 45
How Does This Program Work ?
int a, b, c, sum;
In this program, we have declared four int data type variables named as a, b, c and sum.
// Asking for input cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c;
Then, the user is asked to enter the three numbers.
// Calculating sum sum = a + b + c;
We calculate the sum of these three numbers using the plus (+) operator.
// Displaying output cout << "Sum of three numbers: " << sum << endl;
Finally, the sum of three numbers is displayed on the screen using the cout statement.
Conclusion
I hope after going through this post, you understand how to add three numbers using C++ Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.
Also Read: