You are on page 1of 11

Assignment Questions

1.Write a program to calculate simple interest.


Program:
Def calculate_simple_interest(principal, rate, time):
Interest = (principal * rate * time) / 100
Return interest

# Taking input from the user


Principal = float(input(“Enter the principal amount: “))
Rate = float(input(“Enter the rate of interest: “))
Time = float(input(“Enter the time period (in years): “))

# Calculating simple interest


Simple_interest = calculate_simple_interest(principal, rate, time)

# Displaying the result


print(f” The simple interest is: {simple_interest}”)
Output:
Enter the principal amount: 100000
Enter the rate of interest: 20
Enter the time period (in years): 5
The simple interest is: 100000.0

2.Write a program to check greatest of three numbers


Def find_greatest(num1, num2, num3):
If num1 >= num2 and num1 >= num3:
Return num1
Elif num2 >= num1 and num2 >= num3:
Return num2
Else:
Return num3

# Taking input from the user


Num1 = float(input(“Enter the first number: “))
Num2 = float(input(“Enter the second number: “))
Num3 = float(input(“Enter the third number: “))

# Finding the greatest number


Greatest = find_greatest(num1, num2, num3)

# Displaying the result


print(f”The greatest number is: {greatest}”)
Output:
Enter the first number: 6
Enter the second number: 8
Enter the third number: 45
The greatest number is: 45.0

3.Write a program to check given number is positive or negative?


Def check_positive_negative(num):
If num > 0:
Return “Positive”
Elif num < 0:
Return “Negative”
Else:
Return “Zero”

# Taking input from the user


Num = float(input(“Enter a number: “))

# Checking if the number is positive or negative


Result = check_positive_negative(num)

# Displaying the result


print(f”The number is {result}.”)
Output1:
Enter a number: -7
The number is Negative.
Enter a number:24
The number is Positive

4.Write a program to find smallest number in the list?


Def find_smallest(numbers):
Smallest = numbers[0]
For num in numbers:
If num < smallest:
Smallest = num
Return smallest
No
# Taking input from the user
Numbers = [float(x) for x in input(“Enter a list of numbers separated
by spaces: “).split()]

# Finding the smallest number


Smallest = find_smallest(numbers)

# Displaying the result


print(f”The smallest number in the list is: {smallest}”)
Output:
Enter a list of numbers separated by spaces: 1 2 6 8 9
The smallest number in the list is: 1.0

5.Write a program to check if a string is palindrome or not?


Def is_palindrome(s):
S = s.lower() # Convert to lowercase for case-insensitive
comparison
Return s == s[::-1]
# Taking input from the user
String = input(“Enter a string: “)

# Checking if the string is a palindrome


Result = is_palindrome(string)

# Displaying the result


If result:
print(f”{string} is a palindrome.”)
Else:
print(f”{string} is not a palindrome.”)
Output 1:
Enter a string: TECHNOLOGY
TECHNOLOGY is not a palindrome.
Output 2:
Enter a string: LEVEL
LEVEL is a palindrome

6.Write the program to multiply all numbers in the list?


Def multiply_numbers(numbers):
Result = 1
For num in numbers:
Result *= num
Return result
# Taking input from the user
Numbers = [float(x) for x in input(“Enter a list of numbers separated
by spaces: “).split()]

# Multiplying all the numbers in the list


Product = multiply_numbers(numbers)

# Displaying the result


print(f”The product of all the numbers is: {product}”)
Output:
Enter a list of numbers separated by spaces: 9 6 3 8 2 1
The product of all the numbers is: 2592.0

7.Write a program to find a area of a circle?


Import math

Def calculate_circle_area(radius):
Area = math.pi * (radius**2)
Return area

# Taking input from the user


Radius = float(input(“Enter the radius of the circle: “))

# Calculating the area of the circle


Area = calculate_circle_area(radius)
# Displaying the result
print(f”The area of the circle is: {area}”)
Output:
Enter the radius of the circle: 24
The area of the circle is: 1809.5573684677208

8.Write a program for factorial of a number?


Def factorial(n):
If n == 0 or n == 1:
Return 1
Else:
Return n * factorial(n-1)

# Taking input from the user


Num = int(input(“Enter a non-negative integer: “))

# Checking if the number is non-negative


if num < 0:
print(“Factorial is not defined for negative numbers.”)
else:
# Calculating the factorial
result = factorial(num)
print(f”The factorial of {num} is: {result}”)
Output:
Enter a non-negative integer: 8
The factorial of 8 is: 40320

9.Write a program that reads two numbers using arithmetic operator


and display the computed result.[+,-,*,/]
# Function to perform addition
Def add(x, y):
Return x + y

# Function to perform subtraction


Def subtract(x, y):
Return x – y

# Function to perform multiplication


Def multiply(x, y):
Return x * y

# Function to perform division


Def divide(x, y):
If y != 0:
Return x / y
Else:
Return “Division by zero is not allowed”
# Taking input from the user
Num1 = float(input(“Enter the first number: “))
Num2 = float(input(“Enter the second number: “))

# Performing arithmetic operations


sum_result = add(num1, num2)
difference_result = subtract(num1, num2)
product_result = multiply(num1, num2)
division_result = divide(num1, num2)

# Displaying the results


print(f”Sum: {sum_result}”)
print(f”Difference: {difference_result}”)
print(f”Product: {product_result}”)
print(f”Division: {division_result}”)
Output:
Enter the first number: 55
Enter the second number: 87
Sum: 142.0
Difference: -32.0
Product: 4785.0
Division: 0.632183908045977
10.Write a program to check ascending and descending order of using
5 numbers.
def check_order(numbers):
ascending = True
descending = True

for i in range(len(numbers)-1):
if numbers[i] > numbers[i+1]:
ascending = False
elif numbers[i] < numbers[i+1]:
descending = False

return ascending, descending

# Taking input from the user


numbers = [float(x) for x in input(“Enter 5 numbers separated by
spaces: “).split()]

# Checking the order


Is_ascending, is_descending = check_order(numbers)

# Displaying the result


if is_ascending and not is_descending:
print(“The numbers are in ascending order.”)
elif not is_ascending and is_descending:
print(“The numbers are in descending order.”)
else:
print(“The numbers are neither in ascending nor descending
order.”)
Output 1:
Enter 5 numbers separated by spaces: 1 2 3 6 9
The numbers are in ascending order.
Output 2:
Enter 5 numbers separated by spaces: 9 8 5 4 3
The numbers are in descending order.
Output 3:
Enter 5 numbers separated by spaces: 7 3 8 2 6
The numbers are neither in ascending nor descending order.

You might also like