C++

While Loop Sum of Numbers in C++

Today, we are going to get the sum of numbers in C++ programming language. If we want to add two numbers, we can easily add these numbers but what if we want to add 10 numbers or “n” numbers what can we do? The first method is that we can add numbers one by one and get the desired answer but it is time-consuming work in C++ programing language. To solve this problem, there are loops in C++ programming language that we can get the sum of numbers quickly by writing a few lines of code. We are using one of the loop types which is a while loop to get the sum of numbers in C++ programming language.

Introduction:

In the while loop, we first set the condition of the program like we want to add multiple numbers. Then the compiler runs the condition of the while loop if it is true and executes it until the condition gets false. And if the condition is false, the loop will be terminated and print the sum of the numbers.

Syntax:

To get the sum of multiple numbers, we first need to understand how to implement and the writing style of the sum method in C++ programming language. First, we will write the reserved keyword “while”. Then, we open the brackets to set the condition of the while loop and how many times the loop will be executed. In the while loop brackets, we will write the sum variable and add the “n” numbers which will tell the compiler how many times the loop will run and assign it to the sum variable.  We will use the decremental operator to meet the condition until it is false.


Now, let us start delving by developing some examples in the C++ programming language and observing how they perform to comprehend the concept of the sum method utilizing a while loop more effectively.

Example 01:

Here is an example of how to use a while loop in the C++ programming language to find the sum of n numbers. To develop and execute the program, we must have a C++ compiler. Therefore, launch the C++ compiler and begin developing the code.

We always start by opening the C++ compiler and then adding the header files required for the program to run properly. The header files were first added using the syntax “# include<iostream>”. The “#” symbol informs the translator to import the library, the keyword “include” includes the header file in the program, and the term “iostream” denotes receiving information from the user and displaying the information to the user. Next, we were using the “using namespace std” directive to prohibit classes, functions, and variables from reusing the very same namespace throughout the entire program.

#include<iostream>
using namespace std;
int main()
{
    cout << "*---Sum of Numbers using while loop---*" << endl;
   
    int n, sum;
    sum = 0;
    cout << "\nEnter a number: ";
    cin>>n;
    while(n>0)
    {
        sum = sum + n;
        n--;
    }
    cout << "The sum of n numbers are: " << sum;
    return 0;
}

 
After including the library and namespace, we will start the main() function and start developing the code which we want to implement in the C++ program. First, we want to display a message to the user so that the user can easily understand what we are going to do in the program by using the predefined method of C++ language which is the cout() method. We have also used the “endl” statement that is used to enter the new line in the existing code. After this, we have declared two variables named “n” and “sum”. In line 10, we have assigned the value “0” to the “sum” variable. Now, we want to get the value of “n” which we have already declared above from the user so we used the standard cin() method.

In line 13, we will start the while loop to calculate the sum of the “n” numbers in the program. We first write the reserved keyword “while” and set the condition of the program. The condition is “n>0” which means the value that the user entered will be greater than zero then the condition will work. If users enter 0 then the condition will be false and it will display the 0 in the user’s console window. If the condition of the while loop is true, the compiler enters the body of the loop and starts executing them.

First, it will calculate the sum. Then in the next line, it will decrement the value of n which the user has entered until the condition gets false. When the condition of the while loop is false then the loop will be terminated and it will print the sum of the n numbers by using the cout() method in line 18. After completing the code which we want to implement, we will return 0 to the main() function so that the translator ends the execution of the main() function and display the output to the user’s console window.

Here is the output of the implemented program. The user has entered the 8 number which means the value will be added 8 times in the program. The result of the sum is 36 which means first, we have a value is 8 then the compiler checks the condition if it is true, it will assign 8 to the sum, and then it will decrement the n value by 1. Now, the value of n is 7. The condition of the while loop is true again, it will enter into the body of the while loop and add the 7 to the sum which is sum=8+7=15. Now, the value of the sum is 15 and then the loop again decrements the value of n, now the value of n is 6, and so on until the condition of the loop will be false.

Example 02:

Here is another example of calculating the sum using the while loop in the C++ programming language. In this example, we first included the header file so that we can easily use the cin() and cout() methods in the existing program and we used the namespace directive so that we cannot share the same scope in the program.

#include<iostream>
using namespace std;
int main()
{
    cout << "*---Calculating the Sum of n Numbers---*" << endl;
    int n, sum;
    sum = 0;
    cout << "\nHow many times do you want to iterate the loop? ";
    cin >> n;
    cout << "Enter the values to calculate the sum: ";
    int i=0;
    while(i<n)
    {
        int number;
        cin >> number;
        sum += number;
        i++;
    }
    cout << "\nThe sum of the " << n << " values are: " << sum;
    cout << endl;
    return 0;
}

 
We started the main() function and wrote the code which we want to implement in the program. First, we declared two variables of integer type “n” and “sum” and then we initialized 0 to the sum variable. We want to get the value of “n” from the user so we have used the cin() method. Then, we declared an integer type variable “i” initialized 0 to it and starts the while loop.

First, we write the condition of the while loop which is “i<n” which means the condition will work until “i” is less than “n”. In the while loop body, we have declared a variable “values” to get the values from the user so that we calculate the sum of these values. We have incremented the “i” which means it will works until the condition of the loop will be false. After the termination of the loop, it will print the sum of the values by using the cout() method. Let us see the output of the illustration:

Conclusion

In this article, we have learned how to calculate the sum of multiple numbers by using the while loop in the C++ programming language. We have also learned the writing style so that we can easily develop the code. We have implemented some examples with a detailed explanation of every line of the program.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.