C++

While Loop Sum of Numbers C++

You may have calculated the sum of numbers in mathematics many times while learning. The C++ language also supports the sum of numbers through various supported methods. One of those methods is the use of the “while” loop in different ways to calculate the sum. If you are looking for the use of the “while” loop to calculate the sum of numbers, then this article will be a plus in your learning process. So, let’s make a fresh start within Ubuntu 20.04 system and open its shell console by “Ctrl+Alt+T” shortcut. Go through each example within this article.

Let’s create a new C++ file first. Ubuntu provides us with the “touch” instruction to make any type of file. Thus, we have been using it to generate a C++ file named “while.cc”. This file can be directly opened in the Gnu Nano editor from the shell terminal via “nano” instruction. If you don’t want to use nano editor, try opening the file explorer and moving towards the home folder or towards the folder where your file is located. Right-click on the newly created file and tap on the “Open file in Text editor” option to open it within the text editor to make code.

Example 01:

Now, the empty file has been opened. Within this example, we will take a single numerical value from the user as input and increment this value using the “while” loop. We have added the “iostream” header library to initialize our code and make use of some input and output streams. The execution will be started from the main() method and the initialization of two integer variables “val” and “total” has been done within it. We have used the cout object statement to let our user know that he/she has to input some value now. The cin object statement has been used to take input from the user and store it to the variable “val” which was currently null.

Here comes the “while” loop that has been checking the value of variable “val” added by a user as an input i.e., if it is greater than 0. If the “while” condition satisfies, the value of variable “total” which is currently 0, will be incremented by the variable “val” value. Along with that, the value of variable “val” will be decremented by 1. This process will be continued until the value of the “val” variable doesn’t equal to 0 and the sum will be calculated. After the “while” loop ends, the standard “cout” object statement will be used to display the “total” variable value i.e., sum of the value “val” after all iterations. Our main() function got completed here and it’s time to compile the completed program.

We have compiled our code with “G++”, Ubuntu’s compiler for C++ language as per the shown command. After the fruitful compilation, the code has been executed with the “./a.out” instruction of the shell terminal. The user has been asked to input a value and he/she has added 4 as an input. The sum “10” has been calculated within 4 iterations as shown in the comments of the program.

Example 02:

Within our first example, we have taken a single value and incremented it by another variable value to get the sum. Within this example, we will be calculating a sum of a specified number of values added by the user on the shell as an input. Thus, we have been updating the old C++ code again by opening it in the nano editor. Added the “stream” library and started the main() function. Declared two integer variables “val” and “n” and initialized “total” integer variable to 0. The cout statement is here to ask the user for inputting the value of the “n” variable i.e., a total number of values he/she wants to calculate the sum of.

The cin object statement is here to get input from the user as some numerical value and save to the variable “n”. The next cout object statement is here to ask the user for all the number values that he/she wants to calculate a sum. The “while” loop will be initialized to check if the value of variable “n” is greater than 0 i.e., to iterate the loop. Until the value of “n” is not 0, it will continue to get the values from the user as an input within the variable “val” via the “cin” standard statement. The value added by a user in “val” will be an increment to the variable “total” as the sum of values i.e., total = total + val. Along with that, the variable “n” will be decremented by 1 to complete the “while” loop. After the loop ends, the sum of all the values added by the user as input will be displayed on the shell using the “total” variable.

The code will get compiled again with the G++ compiler for C++. On code execution, a user has added several values he/she wants to calculate the sum i.e., 10. After this, the user has added 10 number values separated by space on the shell. In the end, the sum of all the 10 values has been calculated and displayed on the shell i.e., 138.

Example 03:

Let’s have the last example to get the sum of values using the “while” loop. We will calculate the sum of digits in a numerical value through modulus i.e., dddd = d+d+d+d. Within the main() function, a user has been asked to add a numerical value through the cout statement. The user will input some value in the shell via the cin statement that will store the input in the “n” variable.

The next cout statement is letting us know that the sum of digits in value just added will be calculated. For this, the “while” loop is here to check if the value “n” added by a user must be greater than 0. The modulus of value “n” will be calculated by dividing it to value 10 and the resultant modulus will be saved to variable “val”. The value of variable “val” will be incremented in the variable “total” value as a sum. The value “n” will be divided by 10 and the resultant will be saved again to variable “n”. The whole procedure has been done to convert the inputted number into digits and to sum up the digits together. After the “while” loop, the sum of digits in a number will be displayed i.e., total.

On code execution, a user has added 9845 as a value. The sum of its digits 9, 8, 4, and 5 have been calculated and displayed i.e., 9+8+4+5 = 26.

Conclusion

This article includes some of the very simple methods to calculate the sum of numbers via the while loop. We have used a single value to calculate the sum in a while and used the number of input values to calculate the sum. Also, we have tried to find out the sum of digits in a single number through the while loop. We have covered the examples and methods, we thought would be best for our users.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.