In this post, we will learn how to add five numbers using C++ Programming language.
So, without any delay, let’s begin this tutorial.
C++ Program to Add Five Numbers
// C++ Program to Add Five Numbers #include <iostream> using namespace std; int main(){ int num, sum = 0; // Asking for input cout << "Enter five numbers: " << endl; for (int i = 0; i < 5; i++){ cin >> num; sum = sum + num; } // Displaying output cout << "Sum of Five Numbers: " << sum << endl; return 0; }
Output
Enter five numbers:
1
2
3
4
5
Sum of Five Numbers: 15
How Does This Program Work ?
int num, sum = 0;
In this program, we have declared two int data type variables named num and sum.
// Asking for input cout << "Enter five numbers: " << endl; for (int i = 0; i < 5; i++){ cin >> num; sum = sum + num; }
Then, the user is asked to enter five numbers and the sum of these 5 numbers is calculated with the help of for loop statement.
// Displaying output cout << "Sum of Five Numbers: " << sum << endl;
Finally, the output is displayed on the screen using the cout statement.
Conclusion
I hope after going through this post, you understand how to add five 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:
If i put negative number the program will only get the sum of the positive number