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:
#include<iostream>
usingnamespace 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;
}
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
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
#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;
}
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:
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?