function

Write a program that accepts a positive integer value n and uses a function to calculate 12

+ 22
+ 32
+ ... + n
2
for the given value n. The result should be returned to the main
function and displayed.
---------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int sum(int n);

int main()
{
int n,sumz;
cin>>n;
sumz=sum(n);
cout<<n<<" "<<sumz<<endl;
return 0;

}

int sum(int n)
{
int f=1;
for(int i=n;i>=1;i++)
f=(f*i)+(f*i);
return f;
}

can u help me to solve the problme...
In your for loop you have it looping forever (i > 1 then increment. If you decrement it'll satisfy the condition) I'm not completely sure what math the function is supposed to be done maybe I'm reading it wrong.
in fact it is suposed to print the total of the square number i.e if 1 enter 2..it must calculate the total of 1 to the square = 2 to the square.... but anyway i have solve the problem already...

int sum(int n)
{
int sum=0,f=1;
for(int i=1;i<=n;i++)
{
f=(i*i);
sum=sum+f;
}
return sum;
}
Topic archived. No new replies allowed.