28.6k views
2 votes
Write a program to compute the sum of the series 12 + 22 + 32 ... such that the sum is as large as possible without exceeding 1000. The program should display how many terms the sum uses. 3) Write a program to ask a number from a user, verify that the number is not negative, and compute its factorial

User Dastan
by
8.6k points

1 Answer

4 votes

Answer:

#include <stdio.h>

int main() {

int n = 0; // variable to store the number of terms

int sum = 2; // variable to store the sum

const int limit = 1000;

const int step = 10;

while (sum <= limit) {

if(sum + step > limit) break;

n++;

sum += step;

}

printf("The sum is: %d\\", sum);

printf("The number of terms used: %d\\", n);

return 0;

}

Step-by-step explanation:

Here's a solution in C. I let chatgpt generate the code but that was wrong, but at least I could use it as a starting point. The output will be:

The sum is: 992

The number of terms used: 99

User Aman
by
8.4k points

No related questions found