Write a program to print square root of every alternate number in the range 1 to 10

Python Decision Making of Python (Article) Decision Making of Python (Program)

570

Here is a Python program to print the square root of every alternate number in the range 1 to 10:

Program:

import math

for num in range(1, 11, 2):
    sqrt = math.sqrt(num)
    print("The square root of", num, "is", sqrt)

Output:

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

Explanation:

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.


This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.