Solving C question possibly requiring while loop

Viewed 47

I have this C question:

"Write a program that reads a positive integer x 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."

From what I understand, I have to print the value of n. I tried using a while loop to find the sum of numbers before x is reached, but my code isn't working properly. Any help? Thanks!

#include<stdio.h>
#include<math.h>

int main(){
//while and for loop?
    int a = 90, n = 1;
    float sum;
    while(sum<a){
        sum += pow(n,2);
        n++;
    }
    printf("%d", n);
    
}
0 Answers
Related