Question: Create a function called sumTo which takes an integer parameter and returns the total of all of the numbers from 1 to that number. For

Create a function calledsumTowhich takes an integer parameter and returns the total of all of the numbers from 1 to that number. For example, if the user entered a 5, the function would calculate 1 + 2 + 3 + 4 + 5 = 15. Keep prompting the user for a number until a zero (0) is entered. Use the following run as an example:

Enter a number: 5 sums to 15 Enter a number: 25 sums to 325 Enter a number: 100 sums to 5050 Enter a number: 0 

====================YOUROUTPUT=====================

0001:Enter~a~number:~5

0002:5~is~15

===================MISMATCHFOUNDONLINE0002:===================

ACTUAL:5~is~15

EXPECTED:5~sums~to~15

#include

#include

using namespace std;

int sumTo(int number) {

int counter = 1;

int total = 0;

while (counter <= number) {

total = total + counter;

counter = counter + 1;

}

return total;

}

int main() {

int answer;

int number = 1;

while (number != 0) {

cout <<"Enter a number: ";

cin >>number;

cout<

if (number != 0) {

answer = sumTo(number);

cout<<" " << number <<" is " << answer<< endl;

}

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!