1

Here is what I'm supposed to do:

Write a program that reads a positive integer and displays the maximum positive integer n for which the sum 1^2 + 2^2 + 3^2 + ... + n^2 is less than the given number.

So far I am only able to just add the sum of all natural numbers until n:

#include <stdio.h>

int main ()
{
    unsigned int n;
    int sum = 0;
    int i;

    sum = 0;

    printf("Print your number");
    scanf("%d", &n);

    for (i = 1; i <= n; ++i)
    {
        sum += i;
    }
    printf("sum = %d", sum);

    return 0;

}

Appreciate the help!

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • If it C code, why did you tag c++? – Coral Kashri Oct 09 '18 at 03:05
  • 1
    Be careful with how you use language tags. Tagging unrelated languages attracts extra eyes, but some of those eyes will be annoyed. – user4581301 Oct 09 '18 at 03:10
  • 1
    Side note: Take care to use the correct conversion specifiers in your format string. Often you won't get a useful, or any, message when you select the wrong one. `scanf("%d", &n);` should be `scanf("%u", &n);` to correctly handle an unsigned integer. – user4581301 Oct 09 '18 at 03:14
  • 1
    Another side note: the `^` sign is Exclusive Or (XOR) operator and not the exponent operator. There is no exponent operator, but there is the `pow` function and simple multiplication. For a square of integers, multiplication is the better choice. Part of this is performance, `pow` is designed to handle messy work like e to the power of pi and is overkill for the likes of 10 to the power of 2, but often more important is floating point imprecision. For more on that subject, please read [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user4581301 Oct 09 '18 at 03:22

2 Answers2

1

you can try this

#include <stdio.h>

int max_positive_integer(int given_number)
{
    int sum = 0;
    int n = 1;
    while (sum < given_number) {
        sum += n * n;
        n++;
    }

    printf("sum= %d\n", sum);
    return n;
}

int main ()
{
    printf("Print your number:");
    int n;
    scanf("%d", &n);

    int max_integer = max_positive_integer(n);
    printf("max_integer = %d\n", max_integer);
    return 0;
}
sundb
  • 490
  • 2
  • 8
  • At a quick glance, this looks like a reasonable solution. However I recommend highlighting what you changed and explaining why you changed it. As presented this answer is code only, and code only answer tend to produce [Cargo Cult Programmers](https://en.wikipedia.org/wiki/Cargo_cult_programming), sad, pathetic souls who know only how to cut and paste. – user4581301 Oct 09 '18 at 03:17
  • Hmm with input `5`, this answer prints "Print your number: 5 sum= 5 max_integer = 3 "`. I'd expect "max_integer = 1" – chux - Reinstate Monica Oct 09 '18 at 03:27
  • Starting with `int n = 0;` makes more sense with `max_positive_integer(values_less_than_2)`. – chux - Reinstate Monica Oct 09 '18 at 03:31
0

How to sum a series of squares in a loop is the easy part

sum = 0;
for (n=0; TBD; n++) {
  sum += n*n;
}
printf(..., n);

The trick is when to stop given "sum ... is less than the given number."

Code could use

for (n=0; sum + n*n < given_number; n++) {
  sum += n*n;
}
n--;

That works up to a point, yet seems redundant. It has a problem in that sum + i*i may overflow. Perhaps subtract i*i each time as we do not need to report the sum, just n.

for (n=0; n*n < given_number; n++) {
  given_number -= n*n;
}
n--;

What it nice about this is that the compare on the right side gets smaller as n*n increases, yet n*n does not overflow. Note: n will be about the cubic root of given_number


If you want to avoid the loop, research Sum of First n Squares


As @user4581301 commented, use "%u" with unsigned.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256