C++ program to sum up from x to y | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

C++ program to sum up from x to y

Someone please write a code that will prompt the user to enter two numbers and add up the numbers from the first number to the second number e.g if num1 is 1 and num2 is 9, answer= 1 +2+3+4+5+6+7+8+9 Just help me with the steps and method of the loop Thank you🤗

27th Aug 2018, 6:33 PM
Junaid Bashir
Junaid Bashir - avatar
3 Answers
+ 1
Where is your code? Please post your try
27th Aug 2018, 7:08 PM
KrOW
KrOW - avatar
0
Junaid Bashir check which input is smaller using if statement... next you have to loop through smaller one to bigger one and add number into some variable called result...at the end of loop, your result variable in which you were adding all numbers is the output
28th Aug 2018, 1:46 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
you could create a function with a loop. Just create a variable that the function will return (I'll call it result). keep incrementing result by the smaller one plus the loop counter (initialized with zero) until the loop counter reach the different between the bigger one and the smaller one.
28th Aug 2018, 5:33 AM
Gabriel Quintela
Gabriel Quintela - avatar