Sum of Square numbers

Hello Guys, I want to create a program that will show the sum of the squares of the numbers: For example user inputs 4 and 6, The program outputs 4*4+5*5+6*6
The output will be : 77 I did it but it works slightly wrong can you please help me to fix it? Here is my 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
#include<iostream>
using namespace std;
int summ(int a, int b);
int main()
{
    int x,y;
    cout<<"Enter a number: ";
    cin>>x;
    cout<<"Enter a number: ";
    cin>>y;
    summ(x,y);
    cout<<'n';
    system("pause");
    return 0;
}
int summ(int a,int b)
{
    int sum;
    for(a=0;a<=b;a++)
    sum=a*a+b*b;
    cout<<"Output: "<<sum<<endl;
    return sum;
  
}

Thanks in Advance! :)
Does this help?

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
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;

int sum(int n)
{
    int result=0;

    for (int i=1; i<=n; i++)
    {
        result=result+i;
        //OR result+=i;
    }

    return result;
}

int main()
{
    int a;
    int b;

    cout << "enter two numbers: " << endl;
    cin >> a >> b;

    int result=(a+b)*(a-b);

    cout << '(' << a << '+' << b << ')';
    cout << '*';
    cout << '(' << a << '-' << b << ')';
    cout << '=';
    cout << result << endl;

    int x;

    cout << "enter a number: " << endl;
    cin >> x;

    cout << "1+...+" << x << '=';
    cout << sum(x) << endl;

    system("pause");
    return 0;
}

EDIT: OODAMA RASENGAN!!!
Last edited on
Yeah your program works perfectly but I want another thing I asked that the User inputs two numbers and the program find the sum of the squares in between numbers.
For example:
Input: 4
Input: 6
Output : 77
The program calculates: (4*4)+(5*5)+(6*6) and shows the result,
Thanks Anyway :)

*** AS I SEE YOU ALSO FAN OF NARUTO***
m4ster r0shi (=^_^=)/ - Naruto :D
Last edited on
Uzumaki wrote:
but I want another thing

I know. I wanted to give you something that works so that you
can compare it with your own and see what you're not doing right.

Let's unroll my loop (suppose n=5) and see what it does...

1
2
3
4
5
for (int i=1; i<=n; i++)
{
    result=result+i;
    //OR result+=i;
}

Initially, result=0

i=1 -> result=result+1 (now, result is 1)
i=2 -> result=result+2 (now, result is 3)
i=3 -> result=result+3 (now, result is 6)
i=4 -> result=result+4 (now, result is 10)
i=5 -> result=result+5 (now, result is 15)
i=6 -> break

Now, let's unroll your loop (suppose a=4 and b=6) and see what it does...

1
2
for(a=0; a<=b; a++)
    sum=a*a+b*b;

Initially, sum is ??? (you don't initialize it!!!)

a=0 -> sum=a*a+b*b=0*0+6*6 (now, sum is 36)

(oops!!! why overwrite a? wouldn't it be better to
declare a new counter taking values from a to b?)

a=1 -> sum=a*a+b*b=1*1+6*6 (now, sum is 37)
a=2 -> sum=a*a+b*b=2*2+6*6 (now, sum is 40)
a=3 -> sum=a*a+b*b=3*3+6*6 (now, sum is 45)
a=4 -> sum=a*a+b*b=4*4+6*6 (now, sum is 52)
a=5 -> sum=a*a+b*b=5*5+6*6 (now, sum is 61)
a=6 -> sum=a*a+b*b=6*6+6*6 (now, sum is 72)
a=7 -> break

There are at least three things you should fix:

(1) you should initialize sum properly
(2) you shouldn't overwrite a
(3) you should add to sum (not assign to it) during each iteration

EDIT: KONOHA SENPOU!!!
Last edited on
As far as the program goes, why not just do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using std::cout;
using std::cin;

int main(int argc, char** argv)
{
    cout << "Enter two numbers: ";
    int x,y;
    cin >> x >> y;

    int z = 0;
    for(int i = x;i <= y;i++)
    {
        z += (i*i);
    }

    cout << "The sum of the squares between " << x
            << " and " << y << " is: " << z;

    cin.get();
    cin.get();
    return 0;
}
Last edited on
You forgot using std::endl; and a semicolon inside your loop. Otherwise it's fine.

It's fine.

But I doubt this will benefit the OP.
Last edited on
Yeah, sorry. I removed the endl because it was pointless.

The OP showed that he had written code and tried to solve the problem. I'm giving him a solution, but I wrote it in such a way that hopefully, if he turned it in, his professor would know he didn't write it because of the way it's written and he'll have to think about how it works and write his own version.
packetpirate your program runs perfectly but what if user inputs 6 and then 4. Your program shows 0, which is not true.
So I played a little bit on 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
#include<iostream>
using namespace std;
int summ(int a, int b);
int main()
{
    int x,y;
    cout<<"Enter a number: ";
    cin>>x;
    cout<<"Enter a number: ";
    cin>>y;
    summ(x,y);
    cout<<'n';
    system("pause");
    return 0;
}
int summ(int a,int b)
{
    int z=0;
    if(a<b){
    for(int i=a;a<=b;i++)
    z+=i*i;
}
else {
     for(int i=b;i<=a;i++)
     z+=i*i;
     }
cout<<"Output: "<<z<<endl;
    return z;
  }


Now it works for all cases. Thanks for giving me a good idea. :)
Last edited on
Sorry Guys I came across several problems in this code. It doesn't show the first case. When I enter 4 and 6 program shows nothing can you figure it out? Cuz I can't. I need help on that.
You got a severe organizational flaw in your code- your summ code shouldn't do the output AND the calculation. You should just pass it two values, and it should just return a value. The output should be done in main.

Oh, and you check a<=b instead of i<=b. Infinite loop anyone?
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
#include<iostream>
using namespace std;
int summ(int a, int b);
int main()
{
    int x,y;
    cout<<"Enter 1st number: ";
    cin>>x;
    cout<<"Enter 2nd number: ";
    cin>>y;
    cout<<endl;
    cout<<"Output: "<<summ(x,y)<<endl;
    system("pause");
    return 0;
}
int summ(int a,int b)
{
    int sum=0,dummy;
    if(a>b)
        {dummy=a;a=b;b=dummy;}
        
     for(;a<=b;a++)
        sum+=a*a;   
    return sum;
}
Last edited on
Thank you very much hanst99, The Program runs perfectly, :D
Topic archived. No new replies allowed.