The document contains 10 programming questions and solutions. Each question asks the user to write a Python program to perform a specific task such as calculating simple interest, finding the greatest of three numbers, checking if a string is a palindrome, and checking the order of a list of numbers. For each question, the code for the solution is provided along with sample input/output examples.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
43 views
Assignment Questions Answers-1
The document contains 10 programming questions and solutions. Each question asks the user to write a Python program to perform a specific task such as calculating simple interest, finding the greatest of three numbers, checking if a string is a palindrome, and checking the order of a list of numbers. For each question, the code for the solution is provided along with sample input/output examples.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
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(“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 maxima(m,n,o): k=[m,n,o] return max(k) num1=float(input("Enter the first number: ")) num2=float(input("Enter the second number: ")) num3=float(input("Enter the third number: ")) #Calling the function v=maxima(num1,num2,num3) print(“The greatest number is”,v) 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” num = float(input(“Enter a number: “)) result = check_positive_negative(num) print(“The number is ”,result) Output1: Enter a number: -7 The number is Negative. Output 2: Enter a number:24 The number is Positive
4. Write a program to find smallest number in the list?
def minima(y): return min(y) x=list(input("Enter the values seperated with spaces: ").split()) z=minima(x) print(z,"is the smallest number ") 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(string,” is a palindrome.”) else: print(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(“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(“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(“ 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 mum1 = 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(”Sum: “,sum_result) print( ”Difference: “, difference_result) print(”Product: “,product_result) print(”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()] 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.
(Ebook) App Inventor by David Wolber, Hal Abelson, Ellen Spertus, Liz Looney ISBN 9781449397487, 1449397484 - Instantly access the full ebook content in just a few seconds
(Ebook) App Inventor by David Wolber, Hal Abelson, Ellen Spertus, Liz Looney ISBN 9781449397487, 1449397484 - Instantly access the full ebook content in just a few seconds