[go: up one dir, main page]

0% found this document useful (0 votes)
2 views4 pages

Lab 2

Uploaded by

drshipray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Lab 2

Uploaded by

drshipray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

FACULTY OF TECHNOLOGY

Computer Engineering
01CE1705 – Programming with Python– Lab Manual

PRACTICAL-2

AIM: a) Write a program to calculate the square root of a


number by Newton's Method.
Source Code:
def square_root(number, epsilon):
if number < 0:
print("Cannot calculate the square root of a negative number.")

guess = number / 2
while True:
new_guess = (guess + number / guess) / 2
if abs(guess - new_guess) < epsilon:
return new_guess
guess = new_guess

# User input for both the number to find a square root and the error
torelence level.
number = float(input("Enter a number(not negative number): "))
epsilon = float(input("Enter the tolerance level (epsilon): "))

# Calculate square root of the entered number,


result = square_root(number, epsilon)

# Print the result


print(f"The square root of {number} is approximately {result:.4f}")

Output:

1
FACULTY OF TECHNOLOGY
Computer Engineering
01CE1705 – Programming with Python– Lab Manual

AIM: b) Write a program for checking whether the given


number is an even number or not.

Source Code:
def check_even(number):
if number % 2 == 0:
return True
else:
return False

# Example usage:
num = int(input("Enter any number: "))
if check_even(num):
print(num, "is an even number.")
else:
print(num, "is an odd number.")

Output:

2
FACULTY OF TECHNOLOGY
Computer Engineering
01CE1705 – Programming with Python– Lab Manual

AIM: c) Write a program using a while loop that asks the


user for a number, and prints a countdown from that number
to zero.

Source Code:
number = int(input("Enter a number: "))
while number >= 0:
print(number)
number -= 1

Output:

AIM: d) Write a program that uses for loop to print all the
odd numbers in the range input by user.

Source Code:
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))

print("Odd numbers within the range:", start, "to", end)


for number in range(start, end + 1):
if number % 2 != 0:
print(number)

3
FACULTY OF TECHNOLOGY
Computer Engineering
01CE1705 – Programming with Python– Lab Manual

Output:

You might also like