Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Function in Python - 084838

The document contains 10 Python programs that demonstrate the use of functions. Each program defines a function to perform a specific task such as calculating the area of a circle, checking if a number is prime, generating the Fibonacci series, etc. The main part of each program calls the function, passes any required arguments, and prints the output. The functions allow code reuse and modular programming.

Uploaded by

Abdul rauf Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Function in Python - 084838

The document contains 10 Python programs that demonstrate the use of functions. Each program defines a function to perform a specific task such as calculating the area of a circle, checking if a number is prime, generating the Fibonacci series, etc. The main part of each program calls the function, passes any required arguments, and prints the output. The functions allow code reuse and modular programming.

Uploaded by

Abdul rauf Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

# FUNCION IN PYTHON

PROGRAM 01

PROGRAM 02
Write a program that calculate the
area of circle using function

import math

def calculate_area(radius):
area = math.pi * radius**2
return area

radius = float(input("Enter the radius of the circle:


"))

circle_area = calculate_area(radius)
print("The area of the circle is:", circle_area)

PROGRAM 03
Write a program to calculate sum
of two numbers using function
def add_numbers(a, b):
sum = a + b
return sum
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = add_numbers(num1, num2)

print("The sum of", num1, "and", num2, "is", result)


PROGRAM 04
Write a program to print the prime
number using function

def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
num = int(input("Enter a positive integer: "))
if is_prime(num):
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")

PROGRAM 05
Write a program to display the factorial
of a number using function
def factorial(n):
if n == 0:
return 1
else:
result = 1
for i in range(1, n+1):
result *= i
return result

num = int(input("Enter a non-negative integer: "))

factorial_result = factorial(num)
print("The factorial of", num, "is", factorial_result)

PROGRAM 06
Program to check the string is palindrome
def is_palindrome(string):
string = string.replace(" ", "").lower()
reversed_string = string[::-1]
if string == reversed_string:
return True
else:
return False

user_string = input("Enter a string: ")

if is_palindrome(user_string):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
PROGRAM 08
Write a program to display the table
of a number using function
def print_table(number):
for i in range(1, 11):
print(number, "x", i, "=", number * i)
num = int(input("Enter a number: "))

# Print the table using the print_table function


print("Table of", num)
print_table(num)
PROGRAM 09
Write a program to display weak day
def print_day_name(day_number):
if day_number == 1:
print("Monday")
elif day_number == 2:
print("Tuesday")
elif day_number == 3:
print("Wednesday")
elif day_number == 4:
print("Thursday")
elif day_number == 5:
print("Friday")
elif day_number == 6:
print("Saturday")
elif day_number == 7:
print("Sunday")
else:
print("Invalid day number")
day_num = int(input("Enter a day number (1-7): "))
print("Day name:")
print_day_name(day_num)

PROGRAM 10
Write a program to generate the fabonacci
series using function
def generate_fibonacci_series(n):
fibonacci_series = []
if n <= 0:
return fibonacci_series
elif n == 1:
fibonacci_series.append(0)
elif n == 2:
fibonacci_series.extend([0, 1])
else:
fibonacci_series.extend([0, 1])
a, b = 0, 1
for i in range(2, n):
fib_num = a + b
fibonacci_series.append(fib_num)
a, b = b, fib_num
return fibonacci_series

Fibonacci series
num_terms = int(input("Enter the number of terms in
the Fibonacci series: "))

generate_fibonacci_series function
fibonacci_series =
generate_fibonacci_series(num_terms)

print("Fibonacci series:")
print(fibonacci_series)

You might also like