Python

Program to Count Divisors of Factorial

Python Program to Count Divisors of Factorial

In the previous article, we have discussed Python Program to Reorder a List According to Given Indexes
Factorial:

The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics:

n! = n * (n – 1) *(n – 2) * . . . . . . . . . . 3 * 2 * 1.

4 != 4 * 3 * 2 *1= 24

Given a number and the task is to count divisors of factorial of a given number.

For example :

Let N=6

The divisors of factorial of a given number{ 6 }: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 30 36 40 45 48 60 72 80 90 120 144 180 240 360 720

The count of divisors of factorial of a given number{ 6 }= 30

Examples:

Example 1:

Input:

Given Number = 7

Output:

The divisors of factorial of a given number{ 7 }:
1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 28 30 35 36 40 42 45 48 56 60 63 70 72 80 84 90 105 112 120 126 140 144 168 180 210 240 252 280 315 336 360 420 504 560 630 720 840 1008 1260 1680 2520 5040 
The count of divisors of factorial of a given number{ 7 }= 60

Example 2:

Input:

Given Number = 4

Output:

The divisors of factorial of a given number{ 4 }:
1 2 3 4 6 8 12 24 
The count of divisors of factorial of a given number{ 4 }= 8

Program to Count Divisors of Factorial in Python

Below are the ways to count divisors of factorial of a given number.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take a variable, initialize it with the value ‘1’, and store it in another variable say ‘factorial’.
  • Loop from 1 to the given number using the for loop.
  • Calculate the factorial of a given number by multiplying the above variable “factorial” with the iterator and store it in the same variable ‘factorial’.
  • Take a variable, initialize it with the value ‘0’, and store it in another variable say ‘c’.
  • Loop from 1 to the above obtained factorial number using the for loop.
  • Check if the value of factorial modulus iterator value is equal to 0 using the if conditional statement.
  • If the statement is true, print the iterator value and increment the value of ‘c’.
  • Store it in the same variable ‘c’.
  • Print the count of divisors of factorial of a given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
num = 5
# Take a variable, initialize it with the value '1', and store it in another
# variable say 'factorial'.
factorl = 1
# Loop from 1 to the given number using the for loop.
for i in range(1, num+1):
 # Calculate the factorial of a given number by multiplying the above variable "factorial"
    # with the iterator and store it in the same variable 'factorial'.
    factorl = factorl*i
# Take a variable, initialize it with the value '0', and store it in another variable
# say 'c'.
c = 0
print("The divisors of factorial of a given number{", num, "}:")
# Loop from 1 to the above obtained factorial number using the for loop.
for i in range(1, factorl+1):
  # Check if the value of factorial modulus iterator value is equal to 0 using
    # the if conditional statement.
    if(factorl % i == 0):
     # If the statement is true, print the iterator value and increment the value of 'c'.
        # Store it in the same variable 'c'.
        print(i, end=" ")
        c = c+1
print()
# Print the count of divisors of factorial of a given number.
print("The count of divisors of factorial of a given number{", num, "}=", c)

Output:

The divisors of factorial of a given number{ 5 }:
1 2 3 4 5 6 8 10 12 15 20 24 30 40 60 120 
The count of divisors of factorial of a given number{ 5 }= 16

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Take a variable, initialize it with the value ‘1’, and store it in another variable say ‘factorial’.
  • Loop from 1 to the given number using the for loop.
  • Calculate the factorial of a given number by multiplying the above variable “factorial” with the iterator and store it in the same variable ‘factorial’.
  • Take a variable, initialize it with the value ‘0’, and store it in another variable say ‘c’.
  • Loop from 1 to the above obtained factorial number using the for loop.
  • Check if the value of factorial modulus iterator value is equal to 0 using the if conditional statement.
  • If the statement is true, print the iterator value and increment the value of ‘c’.
  • Store it in the same variable ‘c’.
  • Print the count of divisors of factorial of a given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
num = int(input("Enter some random number = "))
# Take a variable, initialize it with the value '1', and store it in another
# variable say 'factorial'.
factorl = 1
# Loop from 1 to the given number using the for loop.
for i in range(1, num+1):
 # Calculate the factorial of a given number by multiplying the above variable "factorial"
    # with the iterator and store it in the same variable 'factorial'.
    factorl = factorl*i
# Take a variable, initialize it with the value '0', and store it in another variable
# say 'c'.
c = 0
print("The divisors of factorial of a given number{", num, "}:")
# Loop from 1 to the above obtained factorial number using the for loop.
for i in range(1, factorl+1):
  # Check if the value of factorial modulus iterator value is equal to 0 using
    # the if conditional statement.
    if(factorl % i == 0):
     # If the statement is true, print the iterator value and increment the value of 'c'.
        # Store it in the same variable 'c'.
        print(i, end=" ")
        c = c+1
print()
# Print the count of divisors of factorial of a given number.
print("The count of divisors of factorial of a given number{", num, "}=", c)

Output:

Enter some random number = 3
The divisors of factorial of a given number{ 3 }:
1 2 3 6 
The count of divisors of factorial of a given number{ 3 }= 4

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Count Divisors of Factorial Read More »

Program for Product of Maximum in First array and Minimum in Second

Python Program for Product of Maximum in First array and Minimum in Second

In the previous article, we have discussed Python Program to Count Divisors of Factorial
Given two lists, the task is to find the product of maximum in the given first list and minimum in the given second list.

Examples:

Example 1:

Input:

Given the First List = [1, 2, 3, 4, 5]
Given the Second List = [2, 6, 7, 8, 9, 1]

Output:

The maximum element in given first list [1, 2, 3, 4, 5] = 5
The minimum element in given second list [2, 6, 7, 8, 9, 1] = 1
The product of max_in_fstlst and min_in_seclst [ 5 * 1 ] = 5

Example 2:

Input:

Given the First List = [10, 20, 30, 40, 50] 
Given the Second List = [1, 2, 3, 4, 5]

Output:

The maximum element in given first list [10, 20, 30, 40, 50] = 50
The minimum element in given second list [1, 2, 3, 4, 5] = 1
The product of max_in_fstlst and min_in_seclst [ 50 * 1 ] = 50

Program for Product of Maximum in First array and Minimum in Second in Python

Below are the ways to find the product of maximum in the given first list and minimum in the given second list.

Method #1: Using max() and min() Functions (Static Input)

Approach:

  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • Find out the maximum element in the given first list using the built-in max() method and store it in another variable.
  • Find out the minimum element in the given second list using the built-in min() method and store it in another variable.
  • Multiply both the above obtained maximum element in the first list and minimum element in the given second list and store it in another variable.
  • Print the maximum element in the given first .
  • Print the minimum element in the given second list.
  • Print the Product of maximum element in the given first and minimum element in the given second list.
  • The Exit of the Program.

Below is the implementation:

# Give the first list as static input and store it in a variable.
fst_lst = [1, 2, 3, 4, 5]
# Give the second list as static input and store it in another variable.
secnd_lst = [2, 6, 7, 8, 9, 1]
# Find out the maximum element in the given first list using the built-in max() method
# and store it in another variable.
max_in_fstlst = max(fst_lst)
# Find out the minimum element in the given second list using the built-in min() method
# and store it in another variable.
min_in_seclst = min(secnd_lst)
# Multiply both the above obtained maximum element in the first list and minimum element
# in the given second list and store it in another variable.
prodct = max_in_fstlst*min_in_seclst
# Print the maximum element in the given first .
print("The maximum element in given first list", fst_lst, "=", max_in_fstlst)
# Print the minimum element in the given second list.
print("The minimum element in given second list", secnd_lst, "=", min_in_seclst)
# Print the Product of maximum element in the given first and minimum element in
# the given second list.
print("The product of max_in_fstlst and min_in_seclst [",
      max_in_fstlst, "*", min_in_seclst, "] =", prodct)

Output:

The maximum element in given first list [1, 2, 3, 4, 5] = 5
The minimum element in given second list [2, 6, 7, 8, 9, 1] = 1
The product of max_in_fstlst and min_in_seclst [ 5 * 1 ] = 5

Method #2: Using max() and min() Functions (User Input)

Approach:

  • Give the first list as user input using list(),map(),input(),and split() functions and store it in a variable
  • Give the second list as user input using list(),map(),input(),and split() functions and store it in another variable.
  • Find out the maximum element in the given first list using the built-in max() method and store it in another variable.
  • Find out the minimum element in the given second list using the built-in min() method and store it in another variable.
  • Multiply both the above obtained maximum element in the first list and minimum element in the given second list and store it in another variable.
  • Print the maximum element in the given first .
  • Print the minimum element in the given second list.
  • Print the Product of maximum element in the given first and minimum element in the given second list.
  • The Exit of the Program.

Below is the implementation:

# Give the first list as user input using list(),map(),input(),and split() functions and
# store it in a variable
fst_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the second list as user input using list(),map(),input(),and split() functions and
# store it in another variable.
secnd_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Find out the maximum element in the given first list using the built-in max() method
# and store it in another variable.
max_in_fstlst = max(fst_lst)
# Find out the minimum element in the given second list using the built-in min() method
# and store it in another variable.
min_in_seclst = min(secnd_lst)
# Multiply both the above obtained maximum element in the first list and minimum element
# in the given second list and store it in another variable.
prodct = max_in_fstlst*min_in_seclst
# Print the maximum element in the given first .
print("The maximum element in given first list", fst_lst, "=", max_in_fstlst)
# Print the minimum element in the given second list.
print("The minimum element in given second list", secnd_lst, "=", min_in_seclst)
# Print the Product of maximum element in the given first and minimum element in
# the given second list.
print("The product of max_in_fstlst and min_in_seclst [",
      max_in_fstlst, "*", min_in_seclst, "] =", prodct)

Output:

Enter some random List Elements separated by spaces = 4 1 4 2 6 8
Enter some random List Elements separated by spaces = 8 2 4 1 0 -1
The maximum element in given first list [4, 1, 4, 2, 6, 8] = 8
The minimum element in given second list [8, 2, 4, 1, 0, -1] = -1
The product of max_in_fstlst and min_in_seclst [ 8 * -1 ] = -8

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program for Product of Maximum in First array and Minimum in Second Read More »

Program to Replace Every List Element by Multiplication of Previous and Next

Python Program to Replace Every List Element by Multiplication of Previous and Next

In the previous article, we have discussed Python Program for Double Factorial
Given a list and the task is to replace each element of a list by multiplying the previous and next elements.

The first element is replaced by multiplying the first and second elements.

The last element is replaced by multiplying the last and second last elements.

Examples:

Example1:

Input:

Given list = [7, 8, 1, 2, 3, 4]

Output:

The given List after replacing each element of a list by multiplying the previous and next elements:
56 7 16 3 8 12

Explanation:

Given List = [7*8, 7*1, 8*2, 1*3, 2*4, 3*4]

Example2:

Input:

Given list =[ 10 ,1 ,20, 2 ,30, 3]

Output:

The given List after replacing each element of a list by multiplying the previous and next elements:
10 200 2 600 6 90

Explanation:

Given List = [10*1, 10*20, 1*2, 20*30, 2*3, 30*3] = [10, 200, 2, 600, 6, 90]

Program to Replace Every List Element by Multiplication of Previous and Next in Python

Below are the ways to replace each element of a list by multiplying the previous and next elements.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the list using the len() function and store it in another variable.
  • Check if the length of the given list is less than or equal to 1 using the if conditional statement.
  • If the statement is true, do not print anything.
  • Take a variable and initialize it with the first element of the given list say “previous”.
  • Multiply the first and the next element of a list and store it in another variable.
  • Loop from 1 to length of the given list -1 using the for loop.
  • Inside the loop, assign the iterator value of the given list and store it in a
    variable say “currnt”.
  • Multiply the above obtained “previous” with a given list of iterator+1 value
    and store it in the given list of the iterator.
  • Assign the variable “currnt” to the “previus” and come out of the loop.
  • Multiply the above obtained “previous” with a given list of length-1 values and store it in the given list of length-1.
  • print the given list after replacing each element of a list by multiplying the previous and next elements.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [7, 8, 1, 2, 3, 4]
# Calculate the length of the list using the len() function and
# store it in another variable.
lenth = len(gvn_lst)
# Check if the length of the given list is less than or equal to 1 using the
# if conditional statement.
if lenth <= 1:
  # If the statement is true, do not print anything.
    print()
 # Take a variable and initialize it with the first element of the given list
# say "previous".
previus = gvn_lst[0]
# Multiply the first and the next element of a list and store it in another variable.
gvn_lst[0] = gvn_lst[0] * gvn_lst[1]
# Loop from 1 to length of the given list -1 using the for loop.
for itr in range(1, lenth-1):
  # Inside the loop, assign the iterator value of the given list and store it in a
  # variable say "currnt"
    currnt = gvn_lst[itr]
# Multiply the above obtained "previous" with given list of iterator+1 value
# and store it in given list of iterator.
    gvn_lst[itr] = previus * gvn_lst[itr+1]
 # Assign the variable "currnt" to the "previus" and come out of the loop.
    previus = currnt
# Multiply the above obtained "previous" with given list of length-1 value
# and store it in given list of length-1.
gvn_lst[lenth-1] = previus * gvn_lst[lenth-1]
# print the given list after replacing each element of a list by
# multiplying the previous and next elements
print("The given List after replacing each element of a list by multiplying the previous and next elements:")
for itr in range(0, lenth):
    print(gvn_lst[itr], end=" ")

Output:

The given List after replacing each element of a list by multiplying the previous and next elements:
56 7 16 3 8 12

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the list using the len() function and store it in another variable.
  • Check if the length of the given list is less than or equal to 1 using the if conditional statement.
  • If the statement is true, do not print anything.
  • Take a variable and initialize it with the first element of the given list say “previous”.
  • Multiply the first and the next element of a list and store it in another variable.
  • Loop from 1 to length of the given list -1 using the for loop.
  • Inside the loop, assign the iterator value of the given list and store it in a
    variable say “currnt”.
  • Multiply the above obtained “previous” with a given list of iterator+1 value
    and store it in the given list of the iterator.
  • Assign the variable “currnt” to the “previus” and come out of the loop.
  • Multiply the above obtained “previous” with a given list of length-1 values and store it in the given list of length-1.
  • print the given list after replacing each element of a list by multiplying the previous and next elements.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
#Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the list using the len() function and
# store it in another variable.
lenth = len(gvn_lst)
# Check if the length of the given list is less than or equal to 1 using the
# if conditional statement.
if lenth <= 1:
  # If the statement is true, do not print anything.
    print()
 # Take a variable and initialize it with the first element of the given list
# say "previous".
previus = gvn_lst[0]
# Multiply the first and the next element of a list and store it in another variable.
gvn_lst[0] = gvn_lst[0] * gvn_lst[1]
# Loop from 1 to length of the given list -1 using the for loop.
for itr in range(1, lenth-1):
  # Inside the loop, assign the iterator value of the given list and store it in a
  # variable say "currnt"
    currnt = gvn_lst[itr]
# Multiply the above obtained "previous" with given list of iterator+1 value
# and store it in given list of iterator.
    gvn_lst[itr] = previus * gvn_lst[itr+1]
 # Assign the variable "currnt" to the "previus" and come out of the loop.
    previus = currnt
# Multiply the above obtained "previous" with given list of length-1 value
# and store it in given list of length-1.
gvn_lst[lenth-1] = previus * gvn_lst[lenth-1]
# print the given list after replacing each element of a list by
# multiplying the previous and next elements
print("The given List after replacing each element of a list by multiplying the previous and next elements:")
for itr in range(0, lenth):
    print(gvn_lst[itr], end=" ")

Output:

Enter some random List Elements separated by spaces = 10 1 20 2 30 3
The given List after replacing each element of a list by multiplying the previous and next elements:
10 200 2 600 6 90

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Replace Every List Element by Multiplication of Previous and Next Read More »

Program to Reorder a List According to Given Indexes

Python Program to Reorder a List According to Given Indexes

In the previous article, we have discussed Python Program to Find Volume and Surface Area of a Cone
Given two lists of equal length i.e. one is the list of elements and the other of indexes list, the task is to reorder the given list according to the given indexes.

For example:

let Given List = [1, 2, 3, 4, 5]

Given Indexes List = [2, 0, 3, 4, 1]

The given list after reordering according to the given indexes list = [2 5 1 3 4]

The Index list after reordering = [0 1 2 3 4 ]

Examples:

Example1:

Input:

Given List = [1, 2, 3, 4, 5]
Given Index List = [3, 4, 0, 2, 1]

Output:

The given list after reordering according to the given indexes list :
3 5 4 1 2 
The Index list after reordering:
0 1 2 3

Example2:

Input:

Given List =  [10, 20, 30, 40, 50, 60]
Given Index List = [4, 3, 5, 0, 1, 2]

Output:

The given list after reordering according to the given indexes list :
40 50 60 20 10 30 
The Index list after reordering:
0 1 2 3 4 5

Program to Reorder an Array/List According to Given Indexes in Python

Below are the ways to reorder the given list according to the given indexes.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the list of indexes as static input and store it in another variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a list and assign all the elements with 0 of the length of the given list and store it in another variable say “tempry”.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the givenindexlist[itertor value] as index and givenlist[iterator] as value to tempry list.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the given list at the iterator with the “tempry” list at the iterator value.
  • Assign the given list at the iterator index with iterator value.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, Print the iterator value of the given list.
  • Loop from 0 to length of the given index list using the for loop.
  • Inside the loop, Print the iterator value of the given index list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [9, 8, 2, 1]
# Give the list of indexes as static input and store it in another variable.
gvn_indxlst = [1, 3, 0, 2]
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_gvn_lst = len(gvn_lst)
# Take a list and assign all the elements with 0 of the length of the given list and
# store it in another variable say "tempry".
tempry = [0] * len_gvn_lst
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the givenindexlist[itertor value] as index and givenlist[iteratr] as value
    # to temp list
    tempry[gvn_indxlst[itror]] = gvn_lst[itror]
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the given list at the iterator with the "tempry" list at iterator value
    gvn_lst[itror] = tempry[itror]
 # Assign the given list at itertor index with iterator value
    gvn_indxlst[itror] = itror
print("The given list after reordering according to the given indexes list :")
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given list.
    print(gvn_lst[itror], end=" ")
print()
print("The Index list after reordering:")
# Loop from 0 to length of the given index list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given index list.
    print(gvn_indxlst[itror], end=" ")

Output:

The given list after reordering according to the given indexes list :
2 9 1 8 
The Index list after reordering:
0 1 2 3

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the list of indexes as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a list and assign all the elements with 0 of the length of the given list and store it in another variable say “tempry”.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the givenindexlist[itertor value] as index and givenlist[iterator] as value to tempry list.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the given list at the iterator with the “tempry” list at the iterator value.
  • Assign the given list at the iterator index with iterator value.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, Print the iterator value of the given list.
  • Loop from 0 to length of the given index list using the for loop.
  • Inside the loop, Print the iterator value of the given index list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = givnlst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the list of indexes as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_indxlst = givnlst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_gvn_lst = len(gvn_lst)
# Take a list and assign all the elements with 0 of the length of the given list and
# store it in another variable say "tempry".
tempry = [0] * len_gvn_lst
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the givenindexlist[itertor value] as index and givenlist[iteratr] as value
    # to temp list
    tempry[gvn_indxlst[itror]] = gvn_lst[itror]
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the given list at the iterator with the "tempry" list at iterator value
    gvn_lst[itror] = tempry[itror]
 # Assign the given list at itertor index with iterator value
    gvn_indxlst[itror] = itror
print("The given list after reordering according to the given indexes list: ")
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given list.
    print(gvn_lst[itror], end=" ")
print()
print("The Index list after reordering:")
# Loop from 0 to length of the given index list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given index list.
    print(gvn_indxlst[itror], end=" ")

Output:

Enter some random List Elements separated by spaces = 6 4 3 2 1
Enter some random List Elements separated by spaces = 4 0 1 3 2
The given list after reordering according to the given indexes list: 
4 3 1 2 6 
The Index list after reordering:
0 1 2 3 4

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Reorder a List According to Given Indexes Read More »

Program for Alternative Sorting

Python Program for Alternative Sorting

In the previous article, we have discussed Python Program to Sort a List Containing Two Types of Elements
Given a list and the task is to Print the list so that the first element is the first maximum, the second element is the first minimum, and so on.

Examples:

Example1:

Input:

Given List = [2, 3, 7, 8, 1, 3, 6]

Output:

The Alternative sorting of the above given list is :
8 1 7 2 6 3 3

Example2:

Input:

Given List = [2, 1, 7, 4, 1, 2, 5]

Output:

The Alternative sorting of the above given list is :
7 1 5 1 4 2 2

Program for Alternative Sorting in Python

Below are the ways to Print the list so that the first element is the first maximum, the second element is the first minimum, and so on.

Method #1: Using While Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Sort the given list using the sort() method and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with zero.
  • Take another variable say ‘q’ and initialize its value with the length of the given list -1.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, then print the value of the given list of q.
  • Subtract 1 from q (q-1)and store it in the same variable ‘q’.
  • Print the value of the given list of p.
  • Increment the value of p by 1 and store it in the same variable ‘p’.
  • Check if the length of the given list is odd(len_lst % 2 != 0) by using the if conditional statement.
  • If the statement is true, print the value of the given list of p.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [2, 3, 7, 8, 1, 3, 6]
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Sort the given list using the sort() method and store it in another variable.
gvn_lst.sort()
# Take a variable say 'p' and initialize its value with zero.
p = 0
# Take another variable say 'q' and initialize its value with the length of the
# given list -1.
q = len_lst-1
print("The Alternative sorting of the above given list is :")
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, then print the value of the given list of q.
    print(gvn_lst[q], end=" ")
    # Subtract 1 from q (q-1)and store it in the same variable 'q'.
    q -= 1
    # Print the value of the given list of p.
    print(gvn_lst[p], end=" ")
    # Increment the value of p by 1 and store it in the same variable 'p'.
    p += 1
   # Check if the length of the given list is odd(len_lst % 2 != 0) by using the
   # if conditional statement.
if (len_lst % 2 != 0):
    # If the statement is true, print the value of the given list of p.
    print(gvn_lst[p])

Output:

The Alternative sorting of the above given list is :
8 1 7 2 6 3 3

Method #2: Using While loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Sort the given list using the sort() method and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with zero.
  • Take another variable say ‘q’ and initialize its value with the length of the given list -1.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, then print the value of the given list of q.
  • Subtract 1 from q (q-1)and store it in the same variable ‘q’.
  • Print the value of the given list of p.
  • Increment the value of p by 1 and store it in the same variable ‘p’.
  • Check if the length of the given list is odd(len_lst % 2 != 0) by using the if conditional statement.
  • If the statement is true, print the value of the given list of p.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
#Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Sort the given list using the sort() method and store it in another variable.
gvn_lst.sort()
# Take a variable say 'p' and initialize its value with zero.
p = 0
# Take another variable say 'q' and initialize its value with the length of the
# given list -1.
q = len_lst-1
print("The Alternative sorting of the above given list is :")
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, then print the value of the given list of q.
    print(gvn_lst[q], end=" ")
    # Subtract 1 from q (q-1)and store it in the same variable 'q'.
    q -= 1
    # Print the value of the given list of p.
    print(gvn_lst[p], end=" ")
    # Increment the value of p by 1 and store it in the same variable 'p'.
    p += 1
   # Check if the length of the given list is odd(len_lst % 2 != 0) by using the
   # if conditional statement.
if (len_lst % 2 != 0):
    # If the statement is true, print the value of the given list of p.
    print(gvn_lst[p])

Output:

Enter some random List Elements separated by spaces = 2 1 7 4 1 2 5
The Alternative sorting of the above given list is :
7 1 5 1 4 2 2

 Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program for Alternative Sorting Read More »

Program to Sort a List Containing Two Types of Elements

Python Program to Sort a List Containing Two Types of Elements

In the previous article, we have discussed Python Program to Replace Every List Element by Multiplication of Previous and Next
Given a random list of 0s and 1s and the task is to separate the list into 0s on the left and 1s on the right. Only traverse the list once.

Examples:

Example1:

Input:

Given List = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 1 1 1 1 1 1

Example2:

Input:

Given List = [1 0 1 0 0 0 1 1 0 1]

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 0 1 1 1 1 1

Program to Sort a List Containing Two Types of Elements in Python

Below are the ways to separate the given list into 0s on the left and 1s on the right.

Method #1: Using While Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with 0.
  • Take another variable ‘q’ and initialize its value with the length of the given list -1.
  • Store it in another variable.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, check again if the value of the given list of p is equal to 1 using the if conditional statement.
  • If the statement is true, then assign a given list of p with the given list of q and a given list of q with the given list of p.
  • Decrement the value of q by 1 and store it in the same variable ‘q’
  • Else increment the value of p by 1 and store it in the same variable ‘p’.
  • Exit the while Loop.
  • Loop from 0 to the length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
givn_lst = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]
# Calculate the length of the given list using the len() function and store it in
# another variable.
lent_lst = len(givn_lst)
# Take a variable say 'p' and initialize its value with 0.
p = 0
# Take another variable 'q' and initialize its value with the length of the given list -1.
# Store it in another variable.
q = lent_lst - 1
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, check again if the value of the given list of p is equal
    # to 1 using the if conditional statement.
    if(givn_lst[p] == 1):
       # If the statement is true, then assign a given list of p with the given list of q and
       # a given list of q with the given list of p.
        givn_lst[p], givn_lst[q] = givn_lst[q], givn_lst[p]
 # Decrement the value of q by 1 and store it in the same variable 'q
        q -= 1
 # Else increment the value of p by 1 and store it in the same variable 'p'.
 # Exit the while Loop.
    else:
        p += 1
print("The above given list after separation of 0s on the left and 1s on the right:")
# Loop from 0 to the length of the given list using the for loop.
for itror in range(0, lent_lst):
        # Inside the loop, print the given list of iterator values.
    print(givn_lst[itror], end=" ")

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 1 1 1 1 1 1

Method #2: Using While loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with 0.
  • Take another variable ‘q’ and initialize its value with the length of the given list -1.
  • Store it in another variable.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, check again if the value of the given list of p is equal to 1 using the if conditional statement.
  • If the statement is true, then assign a given list of p with the given list of q and a given list of q with the given list of p.
  • Decrement the value of q by 1 and store it in the same variable ‘q’
  • Else increment the value of p by 1 and store it in the same variable ‘p’.
  • Exit the while Loop.
  • Loop from 0 to the length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
givn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and store it in
# another variable.
lent_lst = len(givn_lst)
# Take a variable say 'p' and initialize its value with 0.
p = 0
# Take another variable 'q' and initialize its value with the length of the given list -1.
# Store it in another variable.
q = lent_lst - 1
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, check again if the value of the given list of p is equal
    # to 1 using the if conditional statement.
    if(givn_lst[p] == 1):
       # If the statement is true, then assign a given list of p with the given list of q and
       # a given list of q with the given list of p.
        givn_lst[p], givn_lst[q] = givn_lst[q], givn_lst[p]
 # Decrement the value of q by 1 and store it in the same variable 'q
        q -= 1
 # Else increment the value of p by 1 and store it in the same variable 'p'.
 # Exit the while Loop.
    else:
        p += 1
print("The above given list after separation of 0s on the left and 1s on the right:")
# Loop from 0 to the length of the given list using the for loop.
for itror in range(0, lent_lst):
        # Inside the loop, print the given list of iterator values.
    print(givn_lst[itror], end=" ")

Output:

Enter some random List Elements separated by spaces = 1 0 1 0 0 0 1 1 0 1
The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 0 1 1 1 1 1

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Sort a List Containing Two Types of Elements Read More »

Program for Leonardo Number Series

Python Program for Leonardo Number Series

In the previous article, we have discussed Python Program to Find the Rotation Count in Rotated Sorted List
Leonardo Number:

The Leonardo numbers are a series of numbers determined by recurrence:

L(n) =  1       if n=0

       =   1       if n=1

       =   L(n-1)+L(n-2)+1   if n>1

The first Leonardo numbers are 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361.

The Leonardo numbers are related to the Fibonacci numbers in the following way:

L(n) = 2F(n+1) – 1 if n>=1

Given a number, the task is to find the Leonardo number sequence to the given number.

Examples:

Example1:

Input:

Given Number = 10

Output:

The Leonardo Number Sequence till the given number 10  = 
1
1
3
5
9
15
25
41
67
109

Example2:

Input:

Given Number = 15

Output:

The Leonardo Sequence till the given number 15 = 
1
1
3
5
9
15
25
41
67
109
177
287
465
753
1219

Program for Leonardo Number Series in Python

Below are the ways to find the Leonardo Number Series using the recursive approach in Python:

Method #1: Using Recursion (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as a parameter to the Leonardo recursive function.
  • The base condition is defined as a value that is less than or equal to 1.
  • If the base condition is true, then return 1.
  • Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called recursively with the parameter as the number minus 2 and add 1 to it.
  • Use a for loop to return the Leonardo sequence and return the result and print the result.
  • The Exit of the program.

Below is the implementation :

# function which finds the Leonardo sequence recursively
def LeonardoRecursion(numb):
  # The base condition is defined as a value that is less than or equal to 1.
    if(numb <= 1):
        # If the base condition is true, then return 1.
        return 1
    else:
      # Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called
      # recursively with the parameter as the number minus 2 and add 1 to it.
        return(LeonardoRecursion(numb-1) + LeonardoRecursion(numb-2)+1)


# The user must give the number as static input and store it in a variable.
numb = 10
print("The Leonardo Number Sequence till the given number", numb, ' = ')
# Looping from 1 to given number using for loop
for n in range(numb):
  # passing the iterter value as argument to the recursive function LeonardoRecursion
    print(LeonardoRecursion(n))

Output:

The Leonardo Number Sequence till the given number 10  = 
1
1
3
5
9
15
25
41
67
109

Method #2: Using Recursion (User Input)

Approach:

  • The user must give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as a parameter to the Leonardo recursive function.
  • The base condition is defined as a value that is less than or equal to 1.
  • If the base condition is true, then return 1.
  • Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called recursively with the parameter as the number minus 2 and add 1 to it.
  • Use a for loop to return the Leonardo sequence and return the result and print the result.
  • The exit of the program.

Below is the implementation:

# function which finds the Leonardo sequence recursively
def LeonardoRecursion(numb):
  # The base condition is defined as a value that is less than or equal to 1.
  # If the base condition is true, then return 1.
    if(numb <= 1):
        return 1
    else:
      # Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called
      # recursively with the parameter as the number minus 2 and add 1 to it.
        return(LeonardoRecursion(numb-1) + LeonardoRecursion(numb-2)+1)


# The user must give the number as user input using the
# int(input()) function and store it in a variable.
numb = int(input('Enter some random number = '))
print("The Leonardo Sequence till the given number", numb, ' = ')
# Looping from 1 to given number using for loop
for n in range(numb):
  # passing the iterter value as argument to the recursive function LeonardoRecursion
    print(LeonardoRecursion(n))

Output:

Enter some random number = 15
The Leonardo Sequence till the given number 15 = 
1
1
3
5
9
15
25
41
67
109
177
287
465
753
1219

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program for Leonardo Number Series Read More »

Python Program to Find the Rotation Count in Rotated Sorted List

In the previous article, we have discussed Python Program to Sort a List in Wave Form
Consider a List of distinct numbers that are sorted in ascending order. The list has been rotated k times (clockwise). The task is to determine the value of k.

In simple terms, print the minimum index value to get the rotation count.

Examples:

Example1:

Input:

Given List = [7, 9, 11, 12, 5, 6, 1, 8]

Output:

The number of rotations of a gvn_lst [7, 9, 11, 12, 5, 6, 1, 8] = 6

Example2:

Input:

Given List = [6, 3, 1, 4, 7]

Output:

The number of rotations of a gvn_lst [6, 3, 1, 4, 7] = 1

Program to Find the Rotation Count in Rotated Sorted List in Python

Below are the ways to find the rotation count in the rotated sorted list:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a variable say “minim_index” and initialize it with the first element of the given list.
  • Loop from 0 to the length of the given list using the for loop.
  • Check if the value of the variable “minim_index” is greater than the given list of iterator value.
  • If the statement is true then assign the given list of iterator value to the “minim_index”.
  • Store the iterator value in a variable “minim_index”.
  • Print “minim_index” to get the number of rotations of a given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 3, 5, 7, 9]
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Take a variable say "minim_index" and initialize it with the first element of
# the given list.
minim_index = gvn_lst[0]
# Loop from 0 to the length of the given list using the for loop.
for itr in range(0, len_lst):
 # Check if the value of the variable "minim_index" is greater than the given list of
 # iterator value.
    if (minim_index > gvn_lst[itr]):
        # If the statement is true then assign the given list of iterator value to the "minim_index".
        minim_index = gvn_lst[itr]
# Store the iterator value in a variable "minim_index".
        minim_index = itr
# Print "minim_index" to get the number of rotations of a given list.
print("The number of rotations of a gvn_lst", gvn_lst, "=", minim_index)

Output:

The number of rotations of a gvn_lst [1, 3, 5, 7, 9] = 1

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a variable say “minim_index” and initialize it with the first element of the given list.
  • Loop from 0 to the length of the given list using the for loop.
  • Check if the value of the variable “minim_index” is greater than the given list of iterator value.
  • If the statement is true then assign the given list of iterator value to the “minim_index”.
  • Store the iterator value in a variable “minim_index”.
  • Print “minim_index” to get the number of rotations of a given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
#Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Take a variable say "minim_index" and initialize it with the first element of
# the given list.
minim_index = gvn_lst[0]
# Loop from 0 to the length of the given list using the for loop.
for itr in range(0, len_lst):
 # Check if the value of the variable "minim_index" is greater than the given list of
 # iterator value.
    if (minim_index > gvn_lst[itr]):
        # If the statement is true then assign the given list of iterator value to the "minim_index".
        minim_index = gvn_lst[itr]
# Store the iterator value in a variable "minim_index".
        minim_index = itr
# Print "minim_index" to get the number of rotations of a given list.
print("The number of rotations of a gvn_lst", gvn_lst, "=", minim_index)

Output:

Enter some random List Elements separated by spaces = 6 3 1 4 7
The number of rotations of a gvn_lst [6, 3, 1, 4, 7] = 1

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find the Rotation Count in Rotated Sorted List Read More »

Python Program to Sort a List in Wave Form

In the previous article, we have discussed Python Program for Alternative Sorting.
Waveform:

Sort an unsorted list of integers into a wave-like list given an unsorted list of integers.

If list[0..n-1] >= list[1] <= list[2] >= list[3] <= list[4] >=….., a  ‘list[0..n-1]’ is sorted in wave form.

Examples:

Example1:

Input:

Given List = [1, 5, 7, 2, 4, 6, 9]

Output:

The above given list after sorted in waveform :
2 1 5 4 7 6 9

Example2:

Input:

Given List = [12, 34, 10, 8, 90, 75, 89]

Output:

The above given list after sorted in waveform :
10 8 34 12 89 75 90

Program to Sort a List in Wave Form in Python

Below are the ways to sort a given list in the form of the waveform:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Sort the above-given list using the sort() function.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Loop from 0 to length of the given list with a step size of 2 using the for loop.
  • Inside the loop, assign a given list of (iterator +1) values to the given list of iterator and a given list of the iterator to the given list of iterator+1.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values to sort the list in a waveform.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 5, 7, 2, 4, 6, 9]
# Sort the above-given list using the sort() function.
gvn_lst.sort()
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Loop from 0 to length of the given list with a step size of 2 using the for loop.
for itr in range(0, len_lst-1, 2):
    # Assign a given list of (iterator +1) values to the given list of iterator and
    # a given list of the iterator to the given list of iterator+1.
    gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]
print("The above given list after sorted in waveform :")
# Loop from 0 to length of the given list using the for loop.
for itr in range(0, len(gvn_lst)):
  # Inside the loop, print the given list of iterator values to sort the list in a waveform.
    print(gvn_lst[itr], end=' ')

Output:

The above given list after sorted in waveform :
2 1 5 4 7 6 9

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Sort the above-given list using the sort() function.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Loop from 0 to length of the given list with a step size of 2 using the for loop.
  • Assign a given list of (iterator +1) values to the given list of iterator and a given list of the iterator to the given list of iterator+1.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values to sort the list in a waveform.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
#Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Sort the above-given list using the sort() function.
gvn_lst.sort()
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Loop from 0 to length of the given list with a step size of 2 using the for loop.
for itr in range(0, len_lst-1, 2):
    # Assign a given list of (iterator +1) values to the given list of iterator and
    # a given list of the iterator to the given list of iterator+1.
    gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]
print("The above given list after sorted in waveform :")
# Loop from 0 to length of the given list using the for loop.
for itr in range(0, len(gvn_lst)):
  # Inside the loop, print the given list of iterator values to sort the list in a waveform.
    print(gvn_lst[itr], end=' ')

Output:

Enter some random List Elements separated by spaces = 15 16 17 1 2 3
The above given list after sorted in waveform :
2 1 15 3 17 16

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Sort a List in Wave Form Read More »

Program to Find sum of Even Factors of a Number

Python Program to Find sum of Even Factors of a Number

In the previous article, we have discussed Python Program to Print Number in Ascending Order which contains 1, 2 and 3 in their Digits.
Given a number, and the task is to get the sum of even factors of a given number.

Factors are numbers or algebraic expressions that divide another number by themselves and leave no remainder.

Example: let the given number = 24

# The factors of 24 are : 1, 2, 3, 4, 6, 8, 12, 24

The sum of even factors of 24 = 2+4+6+ 8+12+24 = 56

Examples:

Example1:

Input:

Given Number = 60

Output:

The Sum of all even factors of { 60 }  =  144

Example2:

Input:

Given Number = 24

Output:

The Sum of all even factors of { 24 }  =  56

Program to Find the Sum of Even Factors of a Number in Python

Below are the ways to the sum of even factors of a given number.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take an empty list and store it in another variable.
  • Loop from ‘1’  to the above-given number range using For loop.
  • Check whether the given number modulus iterator value is equal to ‘0’ or not using if conditional statement.
  • If the statement is True, Check if the iterator modulus 2 is equal to 0 using the if conditional statement.
  • If the statement is True, append the iterator value to the above-declared list.
  • Get the sum of all the even factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the even factors of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 24
# Take an empty list and store it in another variable.
all_factors = []
# Loop from '1' to above given number range using For loop.
for itr in range(1, gvn_numb+1):
    # Check whether the given number modulus iterator value is equal to '0' or not
    # using if conditional statement.
    if gvn_numb % itr == 0:
# If the statement is True, Check if the iterator modulus 2 is equal to 0 using the
# if conditional statement.
        if itr % 2 == 0:
            # If the statement is True ,append the iterator value to the above declared list .
            all_factors.append(itr)
      # Get the sum of all the even factors of above got list using built-in sum() function
      # and store it in another variable.
reslt = sum(all_factors)
# Print the sum of all even factors of a given number.
print("The Sum of all even factors of {", gvn_numb, "}  = ", reslt)

Output:

The Sum of all even factors of { 24 }  =  56

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • Take an empty list and store it in another variable.
  • Loop from ‘1’  to the above-given number range using For loop.
  • Check whether the given number modulus iterator value is equal to ‘0’ or not using if conditional statement.
  • If the statement is True, Check if the iterator modulus 2 is equal to 0 using the if conditional statement.
  • If the statement is True, append the iterator value to the above-declared list.
  • Get the sum of all the even factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the even factors of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using int(input()) and store it in a variable.
gvn_numb = int(input("Enter some random Number = "))
# Take an empty list and store it in another variable.
all_factors = []
# Loop from '1' to above given number range using For loop.
for itr in range(1, gvn_numb+1):
    # Check whether the given number modulus iterator value is equal to '0' or not
    # using if conditional statement.
    if gvn_numb % itr == 0:
# If the statement is True, Check if the iterator modulus 2 is equal to 0 using the
# if conditional statement.
        if itr % 2 == 0:
            # If the statement is True ,append the iterator value to the above declared list .
            all_factors.append(itr)
# Get the sum of all the even factors of above got list using built-in sum() function
# and store it in another variable.
reslt = sum(all_factors)
# Print the sum of all even factors of a given number.
print("The Sum of all even factors of {", gvn_numb, "}  = ", reslt)

Output:

Enter some random Number = 32
The Sum of all even factors of { 32 } = 62

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Python Program to Find sum of Even Factors of a Number Read More »