i tried to create a program that would get the sum of the squares but i couldn't get the right output. Could u help me debug? and tell which part of my codes where wrong.PLEASE!
:S

#include<iostream.h>
#include<conio.h>
#include<math.h>
int x,num;
int f(int);
main ()
{

clrscr();
    cout<<"Enter a number: ";
    cin>>num;
    cout<<f(num);
getch ();
return 0;
}
int f(int s)
{
for(x=1;x<=s;x++)
{
int ans2;
 int y;
int ans[10];
ans[y]=pow(x,2);
ans2=ans[y]+ans[y];
cout<<ans2;
y++;
}
return 0;
}

Dani AI

Generated

The wrong output in the original snippet came from undefined behavior and simple logic errors rather than a mysterious compiler bug. Key problems were an uninitialized index variable, writing into a fixed-size array for a variable input, computing ans2 by adding the same slot twice instead of accumulating, and using pow (which returns double) for integer squares. clrscr, getch and conio.h are nonstandard. As suggested, moving declarations out of the loop helps, but the straightforward fix is to drop the array entirely and use a properly initialized accumulator.

A clear, portable iterative solution (no conio.h, no arrays, integer-safe multiplication) looks like this:

#include <iostream>
#include <cstdint>

int main() {
    std::int64_t n;
    if (!(std::cin >> n) || n < 0) return 1;
    std::int64_t sum = 0;
    for (std::int64_t x = 1; x <= n; ++x)
        sum += x * x;        // prefer x*x over pow(x,2)
    std::cout << sum << '\n';
    return 0;
}

For large inputs the O(1) formula is faster and exact (use care for overflow):

#include <iostream>
#include <cstdint>

int main() {
    std::int64_t n;
    if (!(std::cin >> n) || n < 0) return 1;
    // sum = n*(n+1)*(2n+1)/6
    __int128 t = (__int128)n * (n + 1) * (2 * n + 1) / 6; // __int128 on GCC/Clang
    std::cout << (long long)t << '\n'; // verify range before casting in real code
    return 0;
}

Notes: adding a space in cout only changes formatting (as hinted) and does not fix logic. Use long long/int64_t or wider types to avoid overflow; for signed 64-bit the sum fits roughly up to n ~ 3.04e6. Validate input and prefer x*x for integer squares to avoid floating-point rounding.

Recommended Answers

All 4 Replies

move lines 20, 21 and 22 up between lines 17 and 18 (outside the loop)

commented: Shortest working answer of the week :) +36

thanks for the help.
i really appreciate the effort,
but i still couldn't get the right output.

I finally got the correct codes.:icon_cheesygrin:
thanks for those who helped....:)

i tried to create a program that would get the sum of the squares but i couldn't get the right output. Could u help me debug? and tell which part of my codes where wrong.PLEASE!


firend
hi...
your program is totally correct
but u r not getting ur output properly
so put

cout<<" "<<ans2;
instead of cout<<ans2;

okay...

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.