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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started