python programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

LAB EXERCISE-SOFTWARE SYSTEM FOUNDATION

Date: 21 October 2024


Time: 03:00 pm to 05:00 pm

Write python program using function for the following. Take input from the user.

1. Addition of Two Numbers

#Addition of Two Numbers using def funtion and taking users input.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
sum = num1 + num2
print("The sum of", num1, "and", num2, "is", sum)

2. Find Maximum of Three Numbers

#Find Maximum of Three Numbers take input from user

def max(num1, num2, num3):


if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
else:
return num3
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
max_num = max(num1, num2, num3)
print("The maximum number is:", max_num)
3. Factorial of a Number

#to find Factorial of a Number by users input


def factorial(x):
if x == 1 or x == 0:
return 1
else:
return (x * factorial(x-1))
num = int(input("enter no-"))
if num < 0:
print("Factorial is not defined for negative numbers.")
result = factorial(num)
print("The factorial of", num, "is", result)

4. Check if a Number is Prime

#primenumber.py
def primenumber(n):
#n=int(input("enter any number"))
if n>1:
for x in range(2, n):
if n % x == 0:
print("n is not a prime number")
break
else:
print(n, 'is a prime number')
else:
print("use a real number")
primenumber(10)

5. Calculate the Area of a Circle

#calculate area of circle taking users input using functions.


def calculate_circle_area():
radius = float(input("Enter the radius of the circle: "))
area = 3.14 * radius * radius
print("The area of the circle is:", area)
6. Convert Celsius to Fahrenheit

def celcius_to_farenheit(celcius):
farenheit = (celcius * 9/5) + 32
return farenheit
celcius = int(input("Enter the temperature in Celcius: "))
farenheit = celcius_to_farenheit(celcius)
print("The temperature in Farenheit is:", farenheit)

7. Fibonacci Sequence

to print fibponacci series using funtion and taking users input.


def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

n = int(input("Enter the number of terms: "))


for i in range(n):
print(fibonacci(i), end=" ")
print()

8. Reverse a String

#to print the reverse string using def funtion and taking users input.
def reverse_string():
string = input("Enter a string: ")
reversed_string = string[::-1]
print("The reversed string is:", reversed_string)
reverse_string()
9. Check if a String is a Palindrome

#Check if a String is a Palindrome using def funtion and taking users input.
def is_palindrome(s):
return s == s[::-1] # reverse the string and check if it's equal to the
original string
# Taking user input
string = input("Enter a string: ")
# Checking if the string is a palindrome
if is_palindrome(string):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
#Check if a String is a Palindrome using lambda function and taking users
input.

10. Count Vowels in a String

#Count Vowels in a String using def funtion and taking users input.
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in string:
if char in vowels:
count += 1
print("The number of vowels in the string is:", count)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy