In this python function program, we will create a function to get a list of prime numbers from 1 to 100.
Prime Number: A prime number is a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function prime.
- Use the def keyword to define the function.
- Pass two parameters i.e. starting and ending numbers.
- Create a variable and assign its value equal to 0.
If a number is divisible by 1 and by itself then it is a prime number. - Use for loop with the range function to check whether a number is a prime number or not.
- Add only those numbers to the variable.
- Print the output.
- Pass the starting and ending numbers to the function while calling the function.
def prime(lower,upper):
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
count = 0
for i in range(1,num+1):
if num%i == 0:
count += 1
if count == 2:
print(i,end=" ")
prime(1,100)
Output :
Prime numbers between 1 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Related Articles
Python function program to get a list of odd numbers from 1 to 100.
Python function program to print and accept login credentials.
Python function program to get the addition with the return statement.
Python function program to create a Fruitshop Management system.
Python function program to check whether the given year is a leap year.
Python function program to reverse an integer.