49.5k views
0 votes
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, 16 and 30

User Mike Boers
by
5.1k points

1 Answer

6 votes

Answer:

int i=1 // initialized i to 1

int q=0 // initialized q to 10

while((i*i)<h) // iterating the while loop

{

q=(i*i)+q // calculating the sum of square

i=i+1 //Increment of i

}

Step-by-step explanation:

Following are the description of Program

  • Initialized the variable "i" which is "int" type to 1.
  • Initialized the variable "q" which is "int" type to 0.
  • Iterating the while loop to less then variable "h".
  • In this loop we calculate the sum of square and holding the result in the variable "q".
  • Finally increment the value to "i" by 1 to iterating the loop.
User Mukwux
by
5.0k points