Program to get the factorial of a given number

In this python function program, we will create a function to get the factorial of a given number. A factorial is a mathematical operation that you write like this: n! . It represents the multiplication of all numbers between 1 and n.

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
  1. Create a function fact.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. number to the function.
  4. Create a variable named fact and assign its value equal to 1.
  5. While the input number is greater than 0 multiply the fact by the number.
  6. After each multiplication subtract 1 from the input number.
  7. Print the output.
  8. Pass the number to the function while calling the function.
				
					def fact(n):
    fact = 1
    while n > 0:
        fact *= n
        n -= 1
    print(f"Factorial of {num}: {fact}")
num = int(input("Enter a number: "))
fact(num)
				
			

Output :

				
					Enter a number: 5
Factorial of 5: 120
				
			

Related Articles

create a function with *args as parameters.

get the Fibonacci series up to the given number.

Program to create a function with *args as parameters

In this python function program, we will create a function with *args as parameters. 

What is *args?
*args allows us to pass a variable number of non-keyword arguments to a Python function.

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
  1. Create a function func
  2. Use the def keyword to define the function.
  3. Pass multiple parameters to the function using *args.
    *args is used to pass multiple objects to the function.
  4. Use a for loop to iterate numbers and print the cube of each number.
  5. Pass multiple numbers to the function while calling the function.
				
					def func(*args):
    for num in args:
        print(num**3,end=" ")
func(5,6,8,7)
				
			

Output :

				
					125 216 512 343 
				
			

Related Articles

find the HCF of two numbers.

get the factorial of a given number.

Python function program to find the HCF of two numbers

In this python function program, we will find the HCF of two numbers.

HCF:
HCF stands for Highest Common Factor. HCF of two or more numbers is the greatest factor that divides the numbers.

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
  1. Create a function hcf.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. numbers to the function.
  4. Using an if-else statement check which number is smaller.
  5. Use for loop with the range function to iterate over numbers from 1 to the smaller number.
  6. During iteration check whether the iterated number divides both input numbers, if yes then assign that number to the variable.
  7. After the loop is finished print the output.
  8. Take two numbers as input through the user and pass them to the function while calling the function.
				
					def hcf(num1,num2):
    if num1 > num2:
        smaller = num2
    else:
        smaller = num1

    for i in range(1, smaller+1):
        if((num1 % i == 0) and (num2 % i == 0)):
            hcf = i

    print(f"H.C.F of {num1} and {num2}: {hcf}")
    
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
hcf(num1,num2)
				
			

Output :

				
					Enter number 1: 24
Enter number 2: 54
H.C.F of 24 and 54: 6
				
			

Related Articles

calculate the sum of numbers from 0 to 10.

create a function with *args as parameters.

Program to calculate the sum of numbers from 0 to 10

In this python function program, we will calculate the sum of numbers from 0 to 10.

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
  1. Create a function total
  2. Use the def keyword to define the function.
  3. Create a variable and assign its value equal to 0.
  4. Use a for loop with the range function to iterate over numbers from 1-10.
  5. After each iteration add the numbers to the variable.
  6. Print the sum of numbers between 1-10.
  7. Call the function to get the output.
				
					def total():
    t = 0
    for i in range(1,11):
        t += i
    print("Total: ",t)
    
total()
				
			

Output :

				
					Total:  55
				
			

Related Articles

find the LCM of two numbers.

find the HCF of two numbers.

Python function program to find the LCM of two numbers

In this python function program, we will find the LCM of two numbers.

LCM:
LCM stands for ‘Least Common Multiple’ or the Lowest Common Multiple. The least common multiple (LCM) of two numbers is the lowest possible number that can be divisible by both numbers.

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
  1. Create a function lcm
  2. Use the def keyword to define the function.
  3. Pass two parameter i.e. numbers to the function.
  4. Using an if-else statement check which number is greater.
  5. Use While loop.
  6. Inside the While loop check whether both numbers divide the greater number or not.
  7. If yes then break the loop otherwise, add 1 to the greater number and let the loop continue.
  8. At the end print the output.
  9. Take two numbers as input through the user and pass them to the function while calling the function.
				
					def lcm(num1,num2):
    if num1 > num2:
        greater = num1
    else:
        greater = num2

    while(True):
        if((greater % num1 == 0) and (greater % num2 == 0)):
            lcm = greater
            break
        greater += 1

    print(f"L.C.M of {num1} and {num2}: {lcm}")

num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))   
lcm(num1,num2)
				
			

Output :

				
					Enter number 1: 12
Enter number 2: 20
L.C.M of 12 and 20: 60
				
			

Related Articles

access a function inside a function.

calculate the sum of numbers from 0 to 10.

Python function program to access a function inside a function

In this python function program, we wil access a function inside a function.

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
				
					def test(a):
        def add(b):
                nonlocal a
                a += 0
                return a+b
        return add
func= test(10)
print(func(10))
				
			

Output :

				
					20
				
			

Related Articles

execute a string containing Python code.

find the LCM of two numbers.

Program to execute a string containing Python code

In this python function program, we will execute a string containing Python code.

String: A string is any series of characters that are interpreted literally by a script. For example, “Python”.

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
				
					mycode = 'print("Python")'
code = """
def mutiply(x,y):
    return x*y

print('sqatools')
"""
exec(mycode)
exec(code)
				
			

Output :

				
					Python
sqatools
				
			

Related Articles

create and print a list where the values are squares of numbers between 1 to 10.

access a function inside a function.

Program to create and print a list where the values are squares of numbers

In this python function program, we will create and print a list where the values are squares of numbers. To find the square of a number we need to multiply that number by itself.

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
  1. Create a function square.
  2. Use the def keyword to define the function.
  3. Use a for loop with the range function to iterate over numbers from 1-10.
  4. After each iteration print the square of the number.
  5. To get the square of the number use** “(ex.-2**2).
  6. Print the number and its square.
  7. Call the function to get the output.
				
					def square():
    for i in range(1,11):
        print(i,":",i**2)
        
square()
				
			

Output :

				
					1 : 1
2 : 4
3 : 9
4 : 16
5 : 25
6 : 36
7 : 49
8 : 64
9 : 81
10 : 100
				
			

Related Articles

find the even numbers from a given list.

execute a string containing Python code.

Program to find the even numbers from a given list

In this python function program, we will find the even numbers from a given list.

Even number:
Even numbers are those numbers that can be divided into two equal parts.

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
  1. Create a function even.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. list to the function.
  4. Create an empty list.
  5. Use a for loop to iterate over the numbers in the list.
  6. Use an if-else statement to check whether the number is a even number.
  7. If yes then add it to the empty list.
  8. Print the output.
  9. Create a list and pass that list to the function while calling the function.
				
					def even(list1):
    even_list = []
    for val in list1:
        if val%2 == 0:
            even_list.append(val)
    print(even_list)
            
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even(l)
				
			

Output :

				
					[2, 4, 6, 8]
				
			

Related Articles

take a number as a parameter and checks whether the number is prime or not.

create and print a list where the values are squares of numbers between 1 to 10.

Program that take a number as a parameter and checks whether the number is prime or not

In this python function program, we will take a number as a parameter and checks whether the number is prime or not.

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
  1. Create a function prime
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. number to the function.
  4. Create a count variable and assign its value equal to 0.
  5. Use for loop with the range function to check whether a number is a prime number or not.
  6. If a number divides the input number then add 1 to the count variable.
  7. If a number is divisible by 1 and by itself then it is a prime number.
  8. Print the output.
  9. Take a number as input through the user and pass it to the function while calling the function.

				
					def prime(num):
    count = 0

    for i in range(2,num):
        if num%i == 0:
            count += 1

    if count > 0:
        print("It is not a prime number")
    else:
        print("It is a prime number")
        
num1 =  int(input("Enter a number: "))
prime(num1)
				
			

Output :

				
					Enter a number: 7
It is a prime number
				
			

Related Articles

program that takes a list and returns a new list with unique elements of the first list.

find the even numbers from a given list.