0% found this document useful (0 votes)
10 views3 pages

Python Programs Using Functions

The document contains Python programs that demonstrate various functions, including adding two numbers, finding the square of a number, summing list elements, checking if a number is even or odd, calculating factorials, checking for prime numbers, finding the largest of three numbers, and calculating the average of three numbers. Each function is defined with a specific purpose and includes user input for testing. The examples illustrate basic programming concepts and function usage in Python.

Uploaded by

ponjit.borgohain
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)
10 views3 pages

Python Programs Using Functions

The document contains Python programs that demonstrate various functions, including adding two numbers, finding the square of a number, summing list elements, checking if a number is even or odd, calculating factorials, checking for prime numbers, finding the largest of three numbers, and calculating the average of three numbers. Each function is defined with a specific purpose and includes user input for testing. The examples illustrate basic programming concepts and function usage in Python.

Uploaded by

ponjit.borgohain
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

Python Programs using functions

1.​ Function to Add Two Numbers


def add(a, b):
return a + b

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))
print("Sum =", add(x, y))

2.​ Function to Find Square of a Number


def square(n):
return n * n

num = int(input("Enter a number: "))


print("Square =", square(num))

3.​ Function to Find Sum of List Elements


def list_sum(lst):
total = 0
for i in lst:
total += i
return total

numbers = [1, 2, 3, 4, 5]
print("Sum =", list_sum(numbers))

4.​ Function to Check Even or Odd


def check_even_odd(n):
if n % 2 == 0:
print("Even")
else:
print("Odd")

num = int(input("Enter a number: "))


check_even_odd(num)

5.​ Function to Find Factorial


def factorial(n):
fact = 1
for i in range(1, n+1):
fact = fact * i
return fact

num = int(input("Enter a number: "))


print("Factorial =", factorial(num))

6.​ Function to Check Prime Number


def is_prime(n):
for i in range(2, n):
if n % i == 0:
return "Not Prime"
return "Prime"

num = int(input("Enter a number: "))


print(is_prime(num))

7.​ Function to Find Largest of Three Numbers


def largest(a, b, c):
return max(a, b, c)

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Largest =", largest(a, b, c))

8.​ Function to Find Average of Three Numbers


def average(a, b, c):
return (a + b + c) / 3

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))
z = int(input("Enter third number: "))
print("Average =", average(x, y, z))

You might also like