The program asks to input an integer larger than one. the number is
then passed into a function that calculates the sum of squares from
1 to that integer the output should be the value of the integer and the
sum(labeled).
A negative input value signals the end of the data.


so far I got to the point where I can add all the numbers (sum of squares) of any
number input by the user...
I have to show the numbers being added but I so do not know how to do it :/
so for example if I input this
integer 5
the sum of squares is 55....


what I want to know is how to diplay 1+4+9+16+25 next to the 55 (above).
any idea(s)?.

#include<cmath>
#include<iostream>

using namespace std;

int sumsq(int & number,int & sum,int & i);
void errorcheck(int number);

int main()
{
int number;
int sum;
int i;

	cout << "Enter number (>= 0): ";
	cin >> number;
	errorcheck(number);
	sumsq(number, sum,i);

system("pause");
return 0;
}

int sumsq(int & number,int & sum,int & i)
  {
	do
	{
	  for (i =1; i <= number; i++)
        sum =(number*(number+1)*((2*number)+1))/6;
	  cout<<"the sum of squares is"<<sum<<i++<<endl;
	i++;
	}
	while ( i<=number);
    return sum;
  } // sumsq


void errorcheck(int number)
{
	while (number<=1)
	{cout<<"wrong input..";
	cin.clear();
	cin.ignore(1000);
	
	}

}

Edited 6 Years Ago by mmgoicochea: n/a

The for loop should be like this :

for(i=1;i<n;i++)
{
square=i*i;
sum+=square;
cout<<square;
if(i!=n-1) {cout<<" + ";}
if(i==n-1) {cout<<" = "<<sum;}
}

and there is no need of the do-while loop along with the for loop

Remove all the useless stuff from your program, see this program perfectly does what you want it to do :

#include<iostream.h>
#include<conio.h>

void main()
{
int i=0,n=0,square=0,sum=0;
clrscr();

cout<<"\nEnter the number till which the sum of squares is to be computed : ";
cin>>n;

for(i=1;i<n;i++)
{
square=i*i;
sum+=square;
cout<<square;
if(i!=n-1) {cout<<" + ";}
if(i==n-1) {cout<<" = "<<sum;}
}

getch();
}

Edited 6 Years Ago by n21115: n/a

Rewrite your calculation section so it's doesn't use tricks. Make it straight forward and simply output each calculation.

wow thanks I will try that! ithat code of yours look similar but different to the code I wrote... yours seem to be more efficient :P gotta try it tomorrow... its 2am here and I so need to sleep :D

this is my "neanderthal" c++ version of the neat program you made hahha ... but oh well it runs.. :D

#include<cmath>
#include<iostream>
using namespace std;

void sumsq(int & number,int & sum,int & i,int & square);
void errorcheck(int number,int sum,int i,int & square);
int inpput(int & number);
//functions

int main()
{
  int number;
  int sum;
  int i;
  int square;

//variables

  inpput(number);

  sumsq(number, sum,i,square);
errorcheck( number,sum, i, square);
//function calls
  system("pause");
  return 0;
} 
void sumsq(int & number,int & sum,int & i,int & square)
{

    for (i =1; i < number; i++)  
    {
      square=i*i;
      sum=(number*(number+1)*((2*(number))+1)/6);
          //this is a formula I got.it finds the sum of squares of
          //a given number.

    cout<<square;

    if (i!=number-1)
    {
        cout<<" + ";
    }
    else if(i==number-1) 
        {   
          cout<<" = "<<sum<<endl;

        }

    }
//  function sumsq 

} 

//
void errorcheck(int number,int sum,int i, int & square)
{   

   while(number>0)
   {
       inpput(number);
        sumsq(number, sum,i,square);
       errorcheck(number,sum,i,square);

   }

}
//function errorcheck

int inpput(int & number)
{
    cout<<endl;
    cout << "Enter number : ";
    cin >> number;

    while (number<0)
    {
                                cout<<endl;

        cout<<endl;
        break;
                                break;
    }
return number;
}

Edited 3 Years Ago by pritaeas: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.