where upperLimit is a variable of data type int. When the value of upperLimit is 1000, the program will calculate the sum of first 1000 integers. When the value of upperLimit is 5000, the program will calculate the sum of first 5000 integers. Now we can make it re-usable and more effective by requesting the user to enter the value for upper limit:
We don’t have to change our program every time when the limit changes. For the sum of integers, this program has become generic. We can calculate the sum of any number of integers without changing the program. To make the display statement more understandable, we can change our cout statement as:
Try to write the program.
For previous lesson click here: Repetition Structure (Loop)
For next lesson click here: sum of even numbers with while
the easiest way to learn programming
introduction to programming
While Sample Program
0 comments:
Post a Comment