Create a function called sumTo which takes an integer parame

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 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

Solution

Solution.cpp

#include <iostream>//header file for input output function
#include <cstdlib>//header file for exit function
using namespace std;//it tells the compiler to link stdnamespace
int sumTo(int);//function declaration

int main() {//main function
    int n;
    do
    {
    cout<<\"Enter the number : \";
    cin>>n;//key board inputting
    if (n==0)
    exit(0);
    sumTo(n);//calling function
    }while(n!=0);
    return 0;
}
int sumTo(int n)
{//function definition
    int sum = 0;//sum of n numbers
    for (int i = 1; i <= n; ++i)
    {
        sum += i;
    }
    cout << \"Sum = \" << sum<<endl;
    return 0;
}

output

Enter the number  : 5                                                                                                                                                    

sum s to  Sum = 15                                                                                                                                                       

Enter the number  : 25                                                                                                                                                   

sum s to  Sum = 325                                                                                                                                                      

Enter the number  : 100                                                                                                                                                  

sum s to  Sum = 5050                                                                                                                                                     

Enter the number  : 0

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 example, if the user ente

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site