Write a Python program square_root.py that asks a user for an integer n greater or equal to 1. The program should then print the square root of all the odd numbers between 1 and n (Including n if n is odd). Print the square roots rounded up with no more than two digits after the decimal point.

Respuesta :

Answer:

import math

n = int(input("Enter n: "))

for number in range(1, n+1):

   if number % 2 == 1:

       print("{:.2f}".format(math.sqrt(number)), end=" ")

Explanation:

In order to calculate the square roots of the numbers, import math. Note that math.sqrt() calculates and gives the square root of the given number as a parameter

Ask the user to enter the n

Create a for loop that iterates from 1 to n+1 (Since n is included, you need to write n+1)

Inside the loop, check whether the number is odd or not using the if structure and modulo. If the number is odd, calculate its square root using the math.sqrt() and print the result with two digits after the decimal