Assume there is a variable , h already associated with a positive integer value.
ID: 658199 • Letter: A
Question
Assume there is a variable , h already associated with a positive integer value. Write the code necessary to compute the sum of the perfect squares whose value is less than h , starting with 1 . (A perfect square is an integer like 9 , 16 , 25 , 36 that is equal to the square of another integer (in this case 3*3 , 4*4 , 5*5 , 6*6 respectively).) Associate the sum you compute with the variable q . For example, if h is 19 , you would assign 30 to q because the perfect squares (starting with 1 ) that are less than h are: 1 , 4 , 9 , 16and 30==1+4+9+16 .
Explanation / Answer
#include <stdio.h>
int main() {
int num, square, sum = 0, i = 1;
/* get the input value for n from user */
printf("Enter the value for n:");
scanf("%d", &num);
/* calculate the square of 1st n natural numbers */
while (i <= num) {
square = i * i;
sum = sum + square;
i++;
}
/* print the sum of square of first n natural nos */
printf("Sum of squares of first %d natural"
" numbers is %d ", num, sum);
return 0;
}