0% found this document useful (0 votes)
3 views9 pages

PPL Experiment No-7

The document outlines an experiment to write a Python program that determines if a number is prime using functions. It details the design of the program, including the creation of the is_prime function, user input handling, and the implementation of basic programming constructs. Additionally, it briefly mentions a second problem statement for creating a simple calculator using functions.

Uploaded by

jsshah2006
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)
3 views9 pages

PPL Experiment No-7

The document outlines an experiment to write a Python program that determines if a number is prime using functions. It details the design of the program, including the creation of the is_prime function, user input handling, and the implementation of basic programming constructs. Additionally, it briefly mentions a second problem statement for creating a simple calculator using functions.

Uploaded by

jsshah2006
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
You are on page 1/ 9

Experiment No 7

Aim: Write a python program to understand and use the concept of functions.

Tools Used:
1. Python 3.x interpreter
2. Text editor or Integrated Development Environment (IDE) such as VS Code, PyCharm, or
IDLE
3. (Optional) Compiler for C to compare constructs

Problem Statement 1: Using function, write a Python program to analyze if the input number is
prime or not.

The Prime Number Analyzer program is designed to determine whether a given number is a
prime number using Python functions. A prime number is a natural number greater than 1 that
has no positive divisors other than 1 and itself.

Design and implement a Python program that:


1. A function is_prime(number) is defined to perform the prime-checking logic: a. If the
number is less than or equal to 1, it immediately returns False, as numbers ≤1 are
not prime.
b. For numbers greater than 1, it checks divisibility by all integers from 2 to the
square root of the number (inclusive).
c. If the number is divisible by any of these integers, it is not prime, and the function
returns False.
d. Otherwise, it returns True.

2. Prompts the user to enter the number.

3.The input number is passed to the is_prime function.Based on the function's return value, the
program prints whether the number is prime or not.
The program should demonstrate Python’s such as functions, conditionals (if, else), loops (for),
mathematical operations, user input (input()), output (print()), and return statements to determine
if a number is prime.

Theory:
Python Functions:
Python Functions is a block of statements that return the specific task. The idea is to put some
commonly or repeatedly done tasks together and make a function so that instead of writing the
same code again and again for different inputs, we can do the function calls to reuse code
contained in it over and over again.

Types of Functions in Python


● Built-in library function: These are Standard functions in Python that are available
to use.
● User-defined function: We can create our own functions based on our requirements.

Creating a Function in Python


In Python, we can define a function using the def keyword. Functions can be customized with
various functionalities and properties as needed. The following example demonstrates how to
write a function in Python

e.g # A simple Python function


def fun():
print("Welcome")

Calling a Function in Python


After creating a function in Python we can call it by using the name of the functions Python
followed by parenthesis containing parameters of that particular function. Below is the example
for calling def function Python.

e.g. # A simple Python function


def fun():
print("Welcome to GFG")
# Driver code to call a function
fun()

Functions with Parameters in Python


Functions with parameters allow you to pass values to a function, enabling dynamic behavior
based on the inputs. Parameters are variables defined within the parentheses of a function
declaration and are used to receive arguments provided during the function call.

Syntax-
def function_name(parameter1, parameter2, ...):
"""Optional docstring: Describe the function's purpose."""
# Function body
return result # Optional
e.g def add(num1: int, num2: int) -> int:
"""Add two numbers"""
num3 = num1 + num2
return num3

# Driver code
num1, num2 = 5, 15
ans = add(num1, num2)
print(f"The addition of {num1} and {num2} results {ans}.")

Algorithm:

1..Start the program.

2. Define a function is_prime(number) that checks if a number is prime:

I. If the input number is less than or equal to 1, return False.


II. For all integers i starting from 2 up to the square root of number (inclusive): A.
If number % i == 0, return False (the number is not prime).
III. If no divisors are found, return True (the number is prime).

3.Prompt the user to input a number.

4.Convert the user input to an integer.

5. Pass the input number to the is_prime() function.

6. Based on the result of the function:

I. If the function returns True, print: "<number> is a prime number."


II. If the function returns False, print: "<number> is not a prime number." 7.

End.
Flowchart:
Pseudo Code:
START
Prompt the user: "Enter a number to check if it's prime."

1. Read the input and convert it to an integer number.


2. Call the function is_prime(number).
3. If the function returns True:

Print "<number> is a prime number."

4. Else:

Print "<number> is not a prime number."

END

Problem Statement 1: Using function, write a Python program to analyze if the input number is
prime or not.
Program:
Output -

Problem Statement 2:
Implement a simple Python calculator that takes user input and performs basic arithmetic operations
(addition, subtraction, multiplication, division) using functions.

Program-
Output
Conclusion: Understood the basic programming constructs from C to Python by focusing on
Python's syntax and input/output functions.

You might also like