Calculating sum of the square of numbers between two inputs

This is the question:
Write a function which takes two parameter of integer type and both parameters are positive integer. These number should be provided by the user of your program. The first parameter is lower than the second parameter. You function should be able to calculate the sum of the square of each of the numbers between the two inputs and should display that. Please write a main function to display the working of your function. Call the function at least three times by using some loop in the main function.

This is what I came up with but it is not correct:

#include <iostream>
using namespace std;

void sumsquares(int number1, int number2)
{
int sum = 0;
for(int i = number1; i<number2; i++)
sum =sum+ i*i;
cout<<"The Result is: "<<sum<<endl;
}

int main()
{
int number1, number2;
cout<<"Enter two numbers "<<endl;
cout<<"Enter the first number: ";
cin>>number1;
cout<<"Enter the second number: ";
cin>>number2;
cin.get();

sumsquares(number1, number2);


system("pause");
return 0;
}


I need help really badly
Last edited on
in case anyone wants to know how I solved it.


#include <iostream>

using namespace std;

void sumsquares(int number1, int number2)
{
int total = 0;
for(int i = number1; i<=number2; i++){
total += i*i;}
cout<<"The Result is: "<<total<<endl;
}

int main()
{
int number1, number2;
cout<<"Enter two numbers, where the first is less than the second number "<<endl;
cout<<"Enter the first number: ";
cin>>number1;
cout<<"Enter the second number: ";
cin>>number2;
cin.get();
sumsquares(number1, number2);
cout<<"\n";

cout<<"Enter two numbers, where the first is less than the second number "<<endl;
cout<<"Enter the first number: ";
cin>>number1;
cout<<"Enter the second number: ";
cin>>number2;
cin.get();
sumsquares(number1, number2);
cout<<"\n";

cout<<"Enter two numbers, where the first is less than the second number "<<endl;
cout<<"Enter the first number: ";
cin>>number1;
cout<<"Enter the second number: ";
cin>>number2;
cin.get();
sumsquares(number1, number2);
cout<<"\n";



system("pause");
return 0;
}
Last edited on
You can use a loop to shorten your code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

void sumsquares(int number1, int number2)
{
    int total = 0;
    for(int i = number1; i<=number2; i++){
    total += i*i;}
    cout<<"The Result is: "<<total<<endl;
}

int main()
{
    int number1, number2;
    for(int i=0;i<3;i++)
    {
        cout<<"Enter two numbers, where the first is less than the second number "<<endl;
        cout<<"Enter the first number: ";
        cin>>number1;
        cout<<"Enter the second number: ";
        cin>>number2;
        cin.get();
        sumsquares(number1, number2);
        cout<<"\n";
    }


    system("pause");
    return 0;
}
Topic archived. No new replies allowed.