Ask Question
8 March, 00:45

Assume there is a variable, h that has already been assigned 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). Assign the sum you compute to 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==1+4+9+16.

+5
Answers (1)
  1. 8 March, 01:20
    0
    import math

    h = 19

    total = 0

    for i in range (1, h):

    if math. sqrt (i) - int (math. sqrt (i)) = = 0:

    total + = i

    print (total)

    Explanation:

    *The code is in Python.

    Initialize the h and total

    Create a for loop that iterates from 1 to h. Inside the loop, check if the difference between the square root of a number and the integer part of the square root of that number is equal to 0 (This means the number is a perfect square. For example, if the number is 3, the square root is 1.73 and the integer part of this is 1, the difference is 0.73, not 0. However, if the number is 4, the square root is 2.0 and the integer part is 2, the difference is 0), add that number to the total

    When the loop is done, print the total.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Assume there is a variable, h that has already been assigned a positive integer value. Write the code necessary to compute the sum of the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers