// Sum of Squares and Cubes.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
int n,j;
cout<<"Enter a Number:";
cin>>n;
for(j=1;j<=n;j=j+1)
cout<<j*j<< " ";
cout<< endl;
int a,b,c,sum;
a=n+1;
b=2*n+1;
c=n*a*b;
sum=c/6;
cout<<"the sum is:"<<sum<<endl;
cout<<j*j*j<< " ";
cout<<endl;
int d,e,f,g,h,sum2;
d=n*n;
e=n+1;
f=n+1;
g=e*f;
h=d*g;
sum2=h/4;
cout<< "the sum of cubes is : "<<sum2<<endl;



}

the problem is that only part of square calculation is giving the right output and not for the cube.....for further convienence im pasting the actual question....


5. Write a program that adds up the squares and adds up the cubes of integers from 1 to N, where N is entered by the user:

Upper Limit: 5
The sum of Squares is 55
The sum of Cubes is 225
Do this by using just one loop that generates the integers. Of course, if you really needed to calculate these sums you would use the appropriate formulas:
12 + 22 + 32 + ... + n2 = n(n+1)(2n+1)/6
13 + 23 + 33 + ... + n3 = n2(n+1)2/4
Add these formulas to your program and print out their results as well as that of the explicit summations.

Dani AI

Generated

The output problem is not with the cube formula itself but with how the loop/index and prints are used. After the for loop finishes j equals n+1, so cout << j*j*j prints (n+1)^3 instead of the computed sum. The cube sum calculation in the original code is needlessly split across many temporary variables; the same result is clearer and safer if either accumulated in the loop or computed as the square of the sum of 1..n (as suggested). 's advice to try parts separately is a good debugging step: verify the loop accumulators first, then add the closed-form checks.

A compact, single-loop approach (uses 64-bit to reduce overflow risk):

#include <iostream>
using namespace std;

int main() {
    long long n;
    if (!(cin >> n)) return 0;

    long long sumSq = 0;
    long long sumCube = 0;
    for (long long i = 1; i <= n; ++i) {
        sumSq += i * i;
        sumCube += i * i * i;
    }

    // closed-form checks (safe with 64-bit)
    long long s = n * (n + 1) / 2;     // sum of 1..n
    long long formulaSumSq = n * (n + 1) * (2 * n + 1) / 6;
    long long formulaSumCube = s * s;

    cout << "Sum of squares: " << sumSq << '\n';
    cout << "Sum of cubes:   " << sumCube << '\n';
    cout << "Formula (sq):   " << formulaSumSq << '\n';
    cout << "Formula (cube): " << formulaSumCube << '\n';
    return 0;
}

Notes and cautions: avoid pow for integer squares/cubes (it returns double and can introduce rounding). Use long long if n may be large to avoid overflow. Do not reuse the loop index after the loop for printing values; print the accumulator variables instead. This addresses the bug and keeps the code readable and verifiable.

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

Do the cube part separately on its own first.

That would be my first step if I were you.

i tried that but it is giving errors i have to do it in one loop can u guide me

Member Avatar for Member #46692

I know you have to do it in one but if you can't do it separately then?

Ya know.

Post your code.

line 20!!
shouldnt that be in a loop?
and why do you use so many useless variables(d,e,f,g,h). Just say
d=(n*(n+1))/2
sum2=pow(d,2)//squaring d

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.