Write a program to print square root of every alternate number in the range 1 to 10
Write a program to print square root of every alternate number in the range 1 to 10
Write a program to print square root of every alternate number in the range 1 to 10
Here is a Python program to print the square root of every alternate number in the range 1 to 10:
import math
for num in range(1, 11, 2):
sqrt = math.sqrt(num)
print("The square root of", num, "is", sqrt)
The square root of 1 is 1.0
The square root of 3 is 1.7320508075688772
The square root of 5 is 2.23606797749979
The square root of 7 is 2.6457513110645907
The square root of 9 is 3.0
In this program, we first import the math module, which provides various mathematical functions, including the square root function. We then use a for loop with a step size of 2 to iterate over every alternate number in the range 1 to 10 (i.e., 1, 3, 5, 7, 9). For each number in the range, we calculate its square root using the math.sqrt() function, and then we print out the result using the print() function.
This program demonstrates the sequential flow of execution in Python, where the statements are executed in the order in which they appear in the code. The for loop causes the statements inside the loop (i.e., the calculation of the square root and the printing of the result) to be executed repeatedly for every alternate number in the specified range.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.