Python Program to Calculate EMI

Program to Calculate EMI

In the previous article, we have discussed Python Program to Check if a String is Lapindrome or Not
Estimated Monthly Installment (EMI):

EMI is an abbreviation for Estimated Monthly Installment. It is a set amount of money paid by the customer or borrower to the bank or lender on a set date each month of the year. This amount is deducted from the customer’s or borrower’s account every month for a set number of years until the loan is fully paid off by the customer or borrower to the bank or lender.

Formula :

EMI = (P*R*(1+R)T)/((1+R)T-1)

where P = Principle

T = Time

R = Rate of interest.

Given principle, Rate, Time, and the task are to calculate EMI for the given input values in Python

Examples:

Example1:

Input:

Given Principle = 10000
Given Rate = 8
Given Time = 2

Output:

The EMI for the above given values of P,T,R =  452.2729145618459

Example 2:

Input:

Given Principle = 20000
Given Rate = 15
Given Time = 3

Output:

The EMI for the above given values of P,T,R =  693.3065700838845

Program to Calculate EMI

Below are the ways to Calculate BMI for given values of principle, Rate, Time.

Method #1: Using Mathematical Formula (Static input)

Approach:

  • Give the Principle as static input and store it in a variable.
  • Give the Rate as static input and store it in another variable.
  • Give the Time as static input and store it in another variable.
  • Calculate the given rate using the given rate formula( given rate/(12*100))and store it in the same variable.
  • Calculate the given time using the given time formula (given time *12) and store it in the same variable.
  • Calculate the EMI Value using the above given mathematical formula and store it in another variable.
  • Print the given EMI value for the above-given values of Principle, Rate, Time.
  • The Exit of the program.

Below is the implementation:

# Give the Principle as static input and store it in a variable.
gvn_princpl = 10000
# Give the Rate as static input and store it in another variable.
gvn_rate = 8
# Give the Time as static input and store it in another variable.
gvn_time = 2
# Calculate the given rate using given rate formula( given rate/(12*100))and
# store it in a same variable.
gvn_rate = gvn_rate/(12*100)
# Calculate the given time using given time formula (given time *12) and
# store it in a same variable.
gvn_time = gvn_time*12
# Calculate the EMI Value using the above given mathematical formula and
# store it in another variable.
fnl_Emi = (gvn_princpl*gvn_rate*pow(1+gvn_rate, gvn_time)) / \
    (pow(1+gvn_rate, gvn_time)-1)
# Print the given EMI value for the above given values of Principle,Rate,Time.
print("The EMI for the above given values of P,T,R = ", fnl_Emi)

Output:

The EMI for the above given values of P,T,R =  452.2729145618459

Method #2: Using Mathematical Formula (User input)

Approach:

  • Give the Principle as user input using the float(input()) and store it in a variable.
  • Give the Rate as user input using the float(input()) and store it in another variable.
  • Give the Time as user input using the float(input()) and store it in another variable.
  • Calculate the given rate using the given rate formula( given rate/(12*100))and store it in the same variable.
  • Calculate the given time using the given time formula (given time *12) and store it in the same variable.
  • Calculate the EMI Value using the above given mathematical formula and store it in another variable.
  • Print the given EMI value for the above-given values of Principle, Rate, Time.
  • The Exit of the program.

Below is the implementation:

# Give the Principle as user input using float(input()) and store it in a variable.
gvn_princpl = float(input("Enter some random number = "))
# Give the Rate as user input using float(input()) and store it in another variable.
gvn_rate = float(input("Enter some random number = "))
# Give the Time as user input using float(input()) and store it in another variable.
gvn_time = float(input("Enter some random number = "))
# Calculate the given rate using given rate formula( given rate/(12*100))and
# store it in a same variable.
gvn_rate = gvn_rate/(12*100)
# Calculate the given time using given time formula (given time *12) and
# store it in a same variable.
gvn_time = gvn_time*12
# Calculate the EMI Value using the above given mathematical formula and
# store it in another variable.
fnl_Emi = (gvn_princpl*gvn_rate*pow(1+gvn_rate, gvn_time)) / \
    (pow(1+gvn_rate, gvn_time)-1)
# Print the given EMI value for the above given values of Principle,Rate,Time.
print("The EMI for the above given values of P,T,R = ", fnl_Emi)

Output:

Enter some random number = 20000
Enter some random number = 15
Enter some random number = 2.5
The EMI for the above given values of P,T,R = 803.5708686652767

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 Missing Term of any Arithmetic Progression

Program to Find the Missing Term of any Arithmetic Progression

In the previous article, we have discussed Python Program to Find Strong Numbers in a List
Arithmetic progression:

An Arithmetic progression is a mathematical sequence of numbers in which the difference between the consecutive terms is constant.

In general, an arithmetic sequence looks like this:  a, a+d, a+2d, a+3d,…………….

where a = first term

d= common difference

Formula : d= second term – first term

Given an Arithmetic series, and the task is to find the missing term in the given series.

Examples:

Example 1:

Input:

Given Arithmetic Progression series = [10, 14, 18, 22, 30]

Output:

The missing term in the given Arithmetic Progression series is:
26

Example 2:

Input:

Given Arithmetic Progression series = [100, 300, 400, 500, 600, 700]

Output:

The missing term in the given Arithmetic Progression series is:
200

Program to Find the missing term of any Arithmetic progression

Below are the ways to find the missing term in the given Arithmetic progression series.

Method #1: Using For Loop (Static Input)

Approach: 

  • Give the list ( Arithmetic Progression series) as static input and store it in a variable.
  • Calculate the length of the given series using the built-in len() function and store it in another variable.
  • Calculate the common difference by finding the difference between the list’s last and first terms and divide by N and store it in another variable.
  • Take a variable, initialize it with the first term of the given list and store it in another variable say “first term”.
  • Loop from 1 to the length of the given list using for loop.
  • Inside the loop, check if the difference between the list(iterator) and “first term” is not equal to the common difference using if conditional statement.
  • If the statement is true, print the sum of “first term” and common difference
  • Else update the value of variable “first term”  by iterator value.
  • The Exit of the program.

Below is the implementation:

# Give the list ( Arithmetic Progression series) as static input and store it in a variable.
gvn_lst = [100, 200, 400, 500, 600, 700]
# Calculate the length of the given series using the built-in len() function
# and store it in another variable.
lst_len = len(gvn_lst)
# Calculate the common difference by finding the difference between the list's last and
# first terms and divide by N and store it in another variable.
commn_diff = int((gvn_lst[lst_len-1]-gvn_lst[0])/lst_len)
# Take a variable, initialize it with the first term of the given list and store
# it in another variable say "first term".
fst_term = gvn_lst[0]
# Loop from 1 to the length of the given list using for loop.
print("The missing term in the given Arithmetic Progression series is:")
for itr in range(1, lst_len):
 # Inside the loop, check if the difference between the list(iterator) and "first term"
    # is not equal to the common difference using if conditional statement.
    if gvn_lst[itr]-fst_term != commn_diff:
      # If the statement is true, print the sum of "first term" and common difference
        print(fst_term+commn_diff)
        break
    else:
       # Else update the value of variable "first term"  by iterator value.
        fst_term = gvn_lst[itr]

Output:

The missing term in the given Arithmetic Progression series is:
26

Method #2: Using For Loop (User Input)

Approach: 

  • Give the list ( Arithmetic Progression series) as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Calculate the length of the given series using the built-in len() function and store it in another variable.
  • Calculate the common difference by finding the difference between the list’s last and first terms and divide by N and store it in another variable.
  • Take a variable, initialize it with the first term of the given list and store it in another variable say “first term”.
  • Loop from 1 to the length of the given list using for loop.
  • Inside the loop, check if the difference between the list(iterator) and “first term” is not equal to the common difference using if conditional statement.
  • If the statement is true, print the sum of “first term” and common difference
  • Else update the value of variable “first term”  by iterator value.
  • The Exit of the program.

Below is the implementation:

#Give the list ( Arithmetic Progression series) as user input using list(),map(),input(),and split() 
#functions and 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 series using the built-in len() function
# and store it in another variable.
lst_len = len(gvn_lst)
# Calculate the common difference by finding the difference between the list's last and
# first terms and divide by N and store it in another variable.
commn_diff = int((gvn_lst[lst_len-1]-gvn_lst[0])/lst_len)
# Take a variable, initialize it with the first term of the given list and store
# it in another variable say "first term".
fst_term = gvn_lst[0]
# Loop from 1 to the length of the given list using for loop.
print("The missing term in the given Arithmetic Progression series is:")
for itr in range(1, lst_len):
 # Inside the loop, check if the difference between the list(iterator) and "first term"
    # is not equal to the common difference using if conditional statement.
    if gvn_lst[itr]-fst_term != commn_diff:
      # If the statement is true, print the sum of "first term" and common difference
        print(fst_term+commn_diff)
        break
    else:
       # Else update the value of variable "first term"  by iterator value.
        fst_term = gvn_lst[itr]

Output:

Enter some random List Elements separated by spaces = 100 300 400 500 600 700
The missing term in the given Arithmetic Progression series is:
200

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 Greatest Digit in a Number.

Program to Find the Greatest Digit in a Number.

In the previous article, we have discussed Python Program for Comma-separated String to Tuple
Given a number, and the task is to find the greatest digit in a given number.

Example: Let the number is 149.

The greatest digit in a given number is ‘9’

Examples:

Example1:

Input:

Given number = 639

Output:

The maximum digit in given number { 639 } =  9

Example2:

Input:

Given number = 247

Output:

The maximum digit in given number { 247 } =  7

Program to Find the Greatest Digit in a Number.

Below are the ways to find the greatest digit in a given number.

Method #1: Using list() Function (Static input)

Approach:

  • Give the number as static input and store it in a variable.
  • Convert the given number into the string using the str() function and store it in another variable.
  • Convert the above-obtained string number into a list of digits using the built-in list() method and store it in another variable.
  • Find the maximum list of digits using the built-in max() function and store it in another variable.
  • Print the greatest digit in 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_num = 154
# Convert the given number into string using str() function and
# store it in another variable. 
str_numbr = str(gvn_num)
# Convert the above obtained string number into list of digits using bulit-in list()
# method and store it in another variable.
lst = list(str_numbr)
# Find the maximum of list of digits using bulit-in max() function
# and store it in another variable.
maxim_digit = max(lst)
# Print the greatest digit in a given number.
print("The maximum digit in given number {", gvn_num, "} = ", maxim_digit)

Output:

The maximum digit in given number { 154 } =  5

Method #2: Using list() Function (User input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Convert the given number into the string using the str() function and store it in another variable.
  • Convert the above-obtained string number into a list of digits using the built-in list() method and store it in another variable.
  • Find the maximum list of digits using the built-in max() function and store it in another variable.
  • Print the greatest digit in 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_num = int(input("Enter some random number = "))
# Convert the given number into string using str() function and
# store it in another variable. 
str_numbr = str(gvn_num)
# Convert the above obtained string number into list of digits using bulit-in list()
# method and store it in another variable.
lst = list(str_numbr)
# Find the maximum of list of digits using bulit-in max() function
# and store it in another variable.
maxim_digit = max(lst)
# Print the greatest digit in a given number.
print("The maximum digit in given number {", gvn_num, "} = ", maxim_digit)

Output:

Enter some random number = 183
The maximum digit in given number { 183 } = 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 to Count the Number of Alphabets in a String

Program to Count the Number of Alphabets in a String

In the previous article, we have discussed Python Program to Count the Number of Null elements in a List.

Given a string, the task is to count the Number of Alphabets in a given String.

isalpha() Method:

The isalpha() method is a built-in method for string-type objects. If all of the characters are alphabets from a to z, the isalpha() method returns true otherwise, it returns False.

Examples:

Example1:

Input:

Given String = hello btechgeeks

Output:

The Number of Characters in a given string { hello btechgeeks } =  15

Example 2:

Input:

Given String = good morning btechgeeks

Output:

The Number of Characters in a given string { good morning btechgeeks } =  21

Program to Count the Number of Alphabets in a String

Below are the ways to Count the Number of Alphabets in a String.

Method #1: Using isalpha() Method (Static input)

Approach:

  • Give the String as static input and store it in a variable.
  • Take a variable to say ‘count’ and initialize its value with ‘0’
  • Loop from 0 to the length of the above-given String using For Loop.
  • Inside the loop, check whether if the value of the iterator is an alphabet or using the built-in isalpha() method inside the if conditional statement.
  • If the given condition is true, then increment the above-initialized count value by ‘1’.
  • Print the number of Alphabets in a given string by printing the above count value.
  • The Exit of the program.

Below is the implementation:

# Give the String as static input and store it in a variable.
gvn_str = "hello btechgeeks"
# Take a variable say 'count' and initialize it's value with '0'
count_no = 0
# Loop from 0 to the length of the above given String using For Loop.
for itrtor in gvn_str:
    # Inside the loop, check whether  if the value of iterator is alphabet or
    # using built-in isalpha() method inside the if conditional statement.
    if(itrtor.isalpha()):
     # If the given condition is true ,then increment the above initialized count value by '1'.
    	count_no = count_no+1
# Print the number of Alphabets in a given string by printing the above count value.
print(
    "The Number of Characters in a given string {", gvn_str, "} = ", count_no)

Output:

The Number of Characters in a given string { hello btechgeeks } =  15

Method #2: Using isalpha() Method (User input)

Approach:

  • Give the String as user input using the input() function and store it in the variable.
  • Take a variable to say ‘count’ and initialize its value with ‘0’
  • Loop from 0 to the length of the above-given String using For Loop.
  • Inside the loop, check whether if the value of the iterator is an alphabet or using the built-in isalpha() method inside the if conditional statement.
  • If the given condition is true, then increment the above-initialized count value by ‘1’.
  • Print the number of Alphabets in a given string by printing the above count value.
  • The Exit of the program.

Below is the implementation:

# Give the String as user input using the input()function and store it in the variable.
gvn_str = input("Enter some random String = ")
# Take a variable say 'count' and initialize it's value with '0'
count_no = 0
# Loop from 0 to the length of the above given String using For Loop.
for itrtor in gvn_str:
    # Inside the loop, check whether  if the value of iterator is alphabet or
    # using built-in isalpha() method inside the if conditional statement.
    if(itrtor.isalpha()):
     # If the given condition is true ,then increment the above initialized count value by '1'.
    	count_no = count_no+1
# Print the number of Alphabets in a given string by printing the above count value.
print(
    "The Number of Characters in a given string {", gvn_str, "} = ", count_no)

Output:

Enter some random String = good morning btechgeeks
The Number of Characters in a given string { good morning btechgeeks } = 21

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 get the Last Word from a String

Program to get the Last Word from a String

In the previous article, we have discussed Python Program to Subtract two Complex Numbers

Given a string that contains the words the task is to print the last word in the given string in Python.

Examples:

Example1:

Input:

Given string =hello this is BTechgeeks

Output:

The last word in the given string { hello this is BTechgeeks } is: BTechgeeks

Example2:

Input:

Given string =good morning this is btechgeeks

Output:

The last word in the given string { good morning this is btechgeeks } is: btechgeeks

Program to get the Last Word from a String in Python

Below are the ways to get the last word from the given string in Python.

Method #1: Using split() Method (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Split the words of the given string to a list of words using the built-in split() function.
  • Get the last word from the above list of words using negative indexing and store it in a variable.
  • Print the last word of the given string by printing the above variable.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvnstrng = 'hello this is BTechgeeks'
# Split the words of the given string to a list of words
# using the built-in split() function.
lsttofwrds = gvnstrng.split()
# Get the last word from the above list of words using negative indexing
# and store it in a variable.
lstwrd = lsttofwrds[-1]
# Print the last word of the given string by printing the above variable.
print('The last word in the given string {', gvnstrng, '} is:', lstwrd)

Output:

The last word in the given string { hello this is BTechgeeks } is: BTechgeeks

Method #2: Using split() Method (User Input)

Approach:

  • Give the string as user input using the input() function and store it in the variable.
  • Split the words of the given string to a list of words using the built-in split() function.
  • Get the last word from the above list of words using negative indexing and store it in a variable.
  • Print the last word of the given string by printing the above variable.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in the variable.
gvnstrng = input('Enter some random string = ')
# Split the words of the given string to a list of words
# using the built-in split() function.
lsttofwrds = gvnstrng.split()
# Get the last word from the above list of words using negative indexing
# and store it in a variable.
lstwrd = lsttofwrds[-1]
# Print the last word of the given string by printing the above variable.
print('The last word in the given string {', gvnstrng, '} is:', lstwrd)

Output:

Enter some random string = good morning this is btechgeeks
The last word in the given string { good morning this is btechgeeks } is: btechgeeks

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 Calculate GST

Program to Calculate GST

In the previous article, we have discussed Python Program to Find Median of List
Goods and Services Tax (GST):

GST is an abbreviation for Goods and Services Tax. It is a value-added tax levied on goods and services sold for domestic use or consumption. Customers pay GST to the government when they buy goods and services.

To calculate the GST percentage, first, compute the net GST amount by subtracting the original price from the net price that includes the GST. We will use the GST percent formula after calculating the net GST amount.

Formula:

GST% formula = ((GST Amount * 100)/Original price)

Net price        = Original price + GST amount

GST amount   = Net price – Original price

GST%             = ((GST amount * 100)/Original price)

round() function: round function rounds off to the nearest integer value.

Given the Net price, the original price, and the task is to calculate the GST percentage.3

Examples:

Example1:

Input:

Given original price = 520
Given Net price       = 650

Output:

The GST percent for the above given input net and original prices =  25.0%

Example 2:

Input:

Given original price = 354.80
Given Net price       = 582.5

Output:

The GST percent for the above given input net and original prices =  64.17700112739571%

Program to Calculate GST

Below are the ways to Calculate GST.

Method #1: Using Mathematical Formula (Static input)

Approach:

  • Give the original price as static input and store it in a variable.
  • Give the net price as static input and store it in another variable.
  • Calculate the GST amount by using the above-given formula and store it in another variable.
  • Calculate the given GST percentage by using the above-given formula and store it in another variable.
  • Print the given GST value for the above given original and net prices.
  • The Exit of the program.

Below is the implementation:

# Give the original price as static input and store it in a variable.
gvn_Orignl_amt = 520
# Give the net price as static input and store it in another variable.
gvn_Net_amt = 650
# Calculate the GST amount by using the above given formula and store it in
# another variable.
GST_amnt = gvn_Net_amt - gvn_Orignl_amt
# Calculate the given GST percentage by using the above given formula and
# store it in another variable.
gvn_GST_percnt = ((GST_amnt * 100) / gvn_Orignl_amt)
# Print the given GST value for the above given original and net prices.
print("The GST percent for the above given input net and original prices = ",
      gvn_GST_percnt, end='')
print("%")

Output:

The GST percent for the above given input net and original prices =  25.0%

Method #2: Using Mathematical Formula (User input)

Approach:

  • Give the original price as user input using float(input()) and store it in a variable.
  • Give the net price as user input using float(input()) and store it in another variable.
  • Calculate the GST amount by using the above-given formula and store it in another variable.
  • Calculate the given GST percentage by using the above-given formula and store it in another variable.
  • Print the given GST value for the above given original and net prices.
  • The Exit of the program.

Below is the implementation:

# Give the original price as user input using float(input()) and store it in a variable.
gvn_Orignl_amt = float(input("Enter some random number = "))
# Give the net price as user input using float(input()) and store it in another variable.
gvn_Net_amt = float(input("Enter some random number = "))
# Calculate the GST amount by using the above given formula and store it in
# another variable.
GST_amnt = gvn_Net_amt - gvn_Orignl_amt
# Calculate the given GST percentage by using the above given formula and
# store it in another variable.
gvn_GST_percnt = ((GST_amnt * 100) / gvn_Orignl_amt)
# Print the given GST value for the above given original and net prices.
print("The GST percent for the above given input net and original prices = ",
      gvn_GST_percnt, end='')
print("%")

Output:

Enter some random number = 354.80
Enter some random number = 582.5
The GST percent for the above given input net and original prices = 64.17700112739571%

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 Add Trailing Zeros to String

Program to Add Trailing Zeros to String

In the previous article, we have discussed Python Program to Calculate GST
Given a String, and the task is to add the given number of trialing Zeros to the given String.

ljust() method :

We can add trailing zeros to the string using the ljust() method. It has two parameters, x, and y, where x is the length of the string. What will this method accomplish? It will simply go through a string and then to the length of the string that we require, and if both are not the same, it will pad those zeros that they have passed through a string and then store them in another.

Examples:

Example1:

Input:

Given String = hello btechgeeks
no of zeros to be added = 5

Output:

The above given string = hello btechgeeks
The above Given string with added given number of trailing zeros =  hello btechgeeks00000

Example1:

Input:

Given String = good morning btechgeeks
no of zeros to be added = 10

Output:

Enter some Random String = good morning btechgeeks
The above given string = good morning btechgeeks
Enter some Random Number = 10
The above Given string with added given number of trailing zeros = good morning btechgeeks0000000000

Program to Add Trailing Zeros to String

Below are the ways to add trailing Zeros to the given String

Method #1: Using ljust() Method (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Print the above-given string.
  • Give the number of trailing zeros to be added as static input and store it in another variable.
  • Calculate the length of the above-given string using the built-in len() function.
  • Add it with the given number of zeros and store it in another variable.
  • Add the given number of trailing zeros to the above-given String Using the built-in ljust() method and store it in another variable.
  • Print the above-Given string with added given the number of trailing Zeros.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng = 'hello btechgeeks'
# Print the above given string .
print("The above given string = " + str(gvn_strng))
# Give the number of trailing zeros to be added as static input and store it in another
# variable.
No_trzers = 5
# Calculate the length of the above given string using built- in len() function .
# Add it with the given number of zeros and store it in another variable.
tot_lenth = No_trzers + len(gvn_strng)
# Add the given number of trailing zeros to the above given String Using built-in ljust()
# method and store it in another variable.
finl_reslt = gvn_strng.ljust(tot_lenth, '0')
# Print the above Given string with added given number of trailing Zeros.
print("The above Given string with added given number of trailing zeros = ", finl_reslt)

Output:

The above given string = hello btechgeeks
The above Given string with added given number of trailing zeros =  hello btechgeeks00000

Method #2 : Using ljust() Method (User Input)

Approach:

  • Give the string as User input using the input() function and store it in a variable.
  • Print the above given string .
  • Give the number of trailing zeros to be added as User input using the int(input()) function and store it in another variable.
  • Calculate the length of the above given string using built- in len() function .
  • Add it with the given number of zeros and store it in another variable.
  • Add the given number of trailing zeros to the above given String Using built-in ljust() method and store it in another variable.
  • Print the above Given string with added given number of trailing Zeros.
  • The Exit of the program.

Below is the implementation:

# Give the string as User input  using the input() function and store it in a variable.
gvn_strng = input("Enter some Random String = ")
# Print the above given string .
print("The above given string = " + str(gvn_strng))
# Give the number of trailing zeros to be added as User input using the int(input()) function and store it in another
# variable.
No_trzers = int(input("Enter some Random Number = "))
# Calculate the length of the above given string using built- in len() function .
# Add it with the given number of zeros and store it in another variable.
tot_lenth = No_trzers + len(gvn_strng)
# Add the given number of trailing zeros to the above given String Using built-in ljust()
# method and store it in another variable.
finl_reslt = gvn_strng.ljust(tot_lenth, '0')
# Print the above Given string with added given number of trailing Zeros.
print("The above Given string with added given number of trailing zeros = ", finl_reslt)

Output:

Enter some Random String = good morning btechgeeks
The above given string = good morning btechgeeks
Enter some Random Number = 10
The above Given string with added given number of trailing zeros = good morning btechgeeks0000000000

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 Generate Strong Numbers in an Interval

Program to Generate Strong Numbers in an Interval

In the previous article, we have discussed Python Program to Select a Random Element from a Tuple
Strong number:

A Strong number is a special number in which the total of all digit factorials equals the number itself.

Ex: 145 the sum of factorial of digits = 1 ! + 4 ! +5 ! = 1 + 24 +125

To determine whether a given number is strong or not. We take each digit from the supplied number and calculate its factorial, we will do this for each digit of the number.

We do the sum of factorials once we have the factorial of all digits. If the total equals the supplied number, the given number is strong; otherwise, it is not.

Given a number, the task is to check whether the given number is strong number or not.

Examples:

Example 1:

Input:

Given lower limit range  = 1
Given upper limit  range= 200

Output:

The Strong Numbers in a given range 1 and 200 are :
1 2 145

Example 2:

Input:

Given lower limit range = 100
Given upper limit range= 60000

Output:

The Strong Numbers in a given range 100 and 60000 are :
145 40585

Program to Generate Strong Numbers in an Interval

Below are the ways to generate Strong Numbers in a given interval.

Method #1: Using While loop and factorial() function (Static input)

Approach:

  1. Import math function using import keyword.
  2. Give the lower limit range as static input and store it in a variable.
  3. Give the upper limit range as static input and store it in another variable.
  4. Loop from lower limit range to upper limit range using For loop.
  5. Put the iterator value in a temporary variable called tempNum.
  6. Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
  7. The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
  8. Calculate the factorial of the last_Digit using math.factorial() function
  9. When the factorial of the last digit is found, it should be added to the totalSum = totalSumfactNum
  10. Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is  Itr = Itr /10
  11. Steps 5–8 should be repeated until N > 0.
  12. Check if the totalSum is equal to tempNum using the if conditional statement.
  13. If it is true then print the tempNum(which is the iterator value).
  14. The Exit of the program.

Below is the implementation:

# Import the math module using import keyword.
import math
# Give the lower limit range as static input and store it in a variable.
gvn_lower_lmt = 1
# Give the upper limit range as static input and store it in another variable.
gvn_upper_lmt = 200
# Loop from lower limit range to upper limit range using For loop.
print("The Strong Numbers in a given range",
      gvn_lower_lmt, "and", gvn_upper_lmt, "are :")
for itr in range(gvn_lower_lmt, gvn_upper_lmt+1):
   # Taking a variable totalSum and initializing it with 0
    totalSum = 0
    # Put the iterator value in a temporary variable called tempNum.
    tempNum = itr
    # using while to extract digit by digit of the given iterator value
    while(itr):
        s = 1
        factNum = 1
        # Getting the last digit of the iterator value
        remainder = itr % 10
        # calculating the factorial of the digit(extracted by remainder variable)
        # using math.fatorial function
        factNum = math.factorial(remainder)
        # Adding the factorial to the totalSum
        totalSum = totalSum + factNum
        # Dividing the given itertor value by 10
        itr = itr//10
    # checking if the totalSum is equal to the iterator value
    # if it is true then it is strong number then return true
    if(totalSum == tempNum):
        print(tempNum, end=' ')

Output:

The Strong Numbers in a given range 1 and 200 are :
1 2 145

Method #2: Using While loop and factorial() function (User input)

Approach:

  1. Import math function using the import keyword.
  2. Give the lower limit range as user input using int(input()) and store it in a variable.
  3. Give the upper limit range as user input using int(input()) and store it in another variable.
  4. Loop from lower limit range to upper limit range using For loop.
  5. Put the iterator value in a temporary variable called tempNum.
  6. Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
  7. The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
  8. Calculate the factorial of the last_Digit using math.factorial() function
  9. When the factorial of the last digit is found, it should be added to the totalSum = totalSumfactNum
  10. Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is  Itr = Itr /10
  11. Steps 5–8 should be repeated until N > 0.
  12. Check if the totalSum is equal to tempNum using the if conditional statement.
  13. If it is true then print the tempNum(which is the iterator value).
  14. The Exit of the program.

Below is the implementation:

# Import the math module using import keyword.
import math
#Give the lower limit range as user input using int(input()) and
#store it in a variable.
gvn_lower_lmt = int(input("Enter some random number = "))
#Give the upper limit range as user input using int(input()) and 
#store it in another variable.
gvn_upper_lmt = int(input("Enter some random number = "))
# Loop from lower limit range to upper limit range using For loop.
print("The Strong Numbers in a given range",
      gvn_lower_lmt, "and", gvn_upper_lmt, "are :")
for itr in range(gvn_lower_lmt, gvn_upper_lmt+1):
   # Taking a variable totalSum and initializing it with 0
    totalSum = 0
    # Put the iterator value in a temporary variable called tempNum.
    tempNum = itr
    # using while to extract digit by digit of the given iterator value
    while(itr):
        s = 1
        factNum = 1
        # Getting the last digit of the iterator value
        remainder = itr % 10
        # calculating the factorial of the digit(extracted by remainder variable)
        # using math.fatorial function
        factNum = math.factorial(remainder)
        # Adding the factorial to the totalSum
        totalSum = totalSum + factNum
        # Dividing the given itertor value by 10
        itr = itr//10
    # checking if the totalSum is equal to the iterator value
    # if it is true then it is strong number then return true
    if(totalSum == tempNum):
        print(tempNum, end=' ')

Output:

Enter some random number = 1
Enter some random number = 5000
The Strong Numbers in a given range 1 and 5000 are :
1 2 145

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 Strong Numbers in a List

Program to Find Strong Numbers in a List

In the previous article, we have discussed Python Program to Generate Strong Numbers in an Interval
Strong number:

A Strong number is a special number in which the total of all digit factorials equals the number itself.

Ex: 145 the sum of factorial of digits = 1 ! + 4 ! +5 ! = 1 + 24 +125

To determine whether a given number is strong or not. We take each digit from the supplied number and calculate its factorial, we will do this for each digit of the number.

We do the sum of factorials once we have the factorial of all digits. If the total equals the supplied number, the given number is strong; otherwise, it is not.

Given a list, and the task is to find all the Strong numbers in a given list.

Examples:

Example1:

Input:

Given List = [4, 1, 4, 145]

Output:

The Strong Numbers in a given List are :
1 145

Example2:

Input:

Given List =[5, 10, 650, 40585, 2, 145, 900]

Output:

The Strong Numbers in a given List are :
40585 2 145

Program to Find Strong Numbers in a List

Below are the ways to find all the Strong numbers in a given list.

Method #1: Using While loop and factorial() function (Static input)

Approach:

  1. Import the math module using the import keyword.
  2. Give the list as static input and store it in a variable.
  3. Loop in the above-given list using For Loop.
  4. Put the iterator value in a temporary variable called tempNum.
  5. Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
  6. The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
  7. Calculate the factorial of the last_Digit using math.factorial() function
  8. When the factorial of the last digit is found, it should be added to the totalSum = totalSumfactNum
  9. Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is  Itr = Itr /10
  10. Steps 5–8 should be repeated until N > 0.
  11. Check if the totalSum is equal to tempNum using the if conditional statement.
  12. If it is true then print the tempNum(which is the iterator value).
  13. The Exit of the program.

Below is the implementation:

# Import the math module using import keyword.
import math
# Give the list as static input and store it in a variable.
list1 = [4, 1, 4, 145]
# Loop in the above given list using For Loop.
print("The Strong Numbers in a given List are :")
for i in list1:
   # Taking a variable totalSum and initializing it with 0
    totalSum = 0
    # Put the iterator value in a temporary variable called tempNum.
    tempNum = i
    # using while to extract digit by digit of the given iterator value
    while(i):
        s = 1
        factNum = 1
        # Getting the last digit of the iterator value
        remainder = i % 10
        # calculating the factorial of the digit(extracted by remainder variable)
        # using math.fatorial function
        factNum = math.factorial(remainder)
        # Adding the factorial to the totalSum
        totalSum = totalSum + factNum
        # Dividing the given itertor value by 10
        i = i//10
    # checking if the totalSum is equal to the iterator value
    # if it is true then it is strong number then return true
    if(totalSum == tempNum):
        print(tempNum, end=' ')

Output:

The Strong Numbers in a given List are :
1 145

Method #2: Using While loop and factorial() function (User input)

Approach:

  1. Import the math module using the import keyword.
  2. Give the list as user input using list(),map(),input(),and split() functions and Store it in a variable.
  3. Loop in the above-given list using For Loop.
  4. Put the iterator value in a temporary variable called tempNum.
  5. Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
  6. The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
  7. Calculate the factorial of the last_Digit using math.factorial() function
  8. When the factorial of the last digit is found, it should be added to the totalSum = totalSumfactNum
  9. Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is  Itr = Itr /10
  10. Steps 5–8 should be repeated until N > 0.
  11. Check if the totalSum is equal to tempNum using the if conditional statement.
  12. If it is true then print the tempNum(which is the iterator value).
  13. The Exit of the program.

Below is the implementation:

# Import the math module using import keyword.
import math
#Give the list as user input using list(),map(),input(),and split() functions and 
#Store it in a variable
list1 = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Loop in the above given list using For Loop.
print("The Strong Numbers in a given List are :")
for i in list1:
   # Taking a variable totalSum and initializing it with 0
    totalSum = 0
    # Put the iterator value in a temporary variable called tempNum.
    tempNum = i
    # using while to extract digit by digit of the given iterator value
    while(i):
        s = 1
        factNum = 1
        # Getting the last digit of the iterator value
        remainder = i % 10
        # calculating the factorial of the digit(extracted by remainder variable)
        # using math.fatorial function
        factNum = math.factorial(remainder)
        # Adding the factorial to the totalSum
        totalSum = totalSum + factNum
        # Dividing the given itertor value by 10
        i = i//10
    # checking if the totalSum is equal to the iterator value
    # if it is true then it is strong number then return true
    if(totalSum == tempNum):
        print(tempNum, end=' ')

Output:

Enter some random List Elements separated by spaces = 1 145 10 15 2
The Strong Numbers in a given List are :
1 145 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 to Print all Disarium Numbers within Given range

Program to Print all Disarium Numbers within Given range

In the previous article, we have discussed Python Program to Get Tangent value Using math.tan()
Disarium Number:

A Disarium number is one in which the sum of each digit raised to the power of its respective position equals the original number.

like 135 , 89, etc.

Here 1^1 + 3^2 + 5^3 = 135 so it is disarium Number

Examples:

Example1:

Input:

Given lower limit range = 7
Given upper limit range = 180

Output:

The disarium numbers in the given range 7 and 180 are: 
7 8 9 89 135 175

Example 2:

Input:

Given lower limit range = 1
Given upper limit range = 250

Output:

The disarium numbers in the given range 1 and 250 are:
1 2 3 4 5 6 7 8 9 89 135 175

Program to Print all Disarium Numbers within Given range

Below are the ways to Print all Disarium Numbers within the given range.

Method #1: Using For Loop (Static input)

Approach:

  • Give the lower limit range as static input and store it in a variable.
  • Give the upper limit range as static input and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the for loop take a variable say ‘num’ and initialize its value to the iterator value.
  • Make a copy of the number so you can verify the outcome later.
  • Make a result variable (with a value of 0) and an iterator ( set to the size of the number)
  • Create a while loop to go digit by digit through the number.
  • On each iteration, multiply the result by a digit raised to the power of the iterator value.
  • On each traversal, increment the iterator.
  • Compare the result value to a copy of the original number.
  • Print if the given number is a disarium number.
  • The Exit of the program.

Below is the implementation:

# Give the lower limit range as static input and store it in a variable.
lowlim_range = 7
# Give the upper limit range as static input and store it in another variable.
upplim_range = 180
print('The disarium numbers in the given range',
      lowlim_range, 'and', upplim_range, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for m in range(lowlim_range, upplim_range+1):

    # given number
    num = m
    # intialize result to zero(ans)
    ans = 0

    # calculating the digits
    digit_s = len(str(num))
    # copy the number in another variable(duplicate)
    dup_numbr = num
    while (dup_numbr != 0):
        # getting the last digit
        remaindr = dup_numbr % 10
        # multiply the result by a digit raised to the power of the iterator value.
        ans = ans + remaindr**digit_s
        digit_s = digit_s - 1
        dup_numbr = dup_numbr//10
    # It is disarium number if it is equal to original number
    if(num == ans):
        print(num, end=' ')

Output:

The disarium numbers in the given range 7 and 180 are:
7 8 9 89 135 175

Method #2: Using For Loop (User input)

Approach:

  • Give the lower limit range as user input using the int(input()) function and store it in a variable.
  • Give the upper limit range as user input using the int(input()) function and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the for loop take a variable say ‘num’ and initialize it’s value to  iterator value .
  • Make a copy of the number so you can verify the outcome later.
  • Make a result variable (with a value of 0) and an iterator ( set to the size of the number)
  • Create a while loop to go digit by digit through the number.
  • On each iteration, multiply the result by a digit raised to the power of the iterator value.
  • On each traversal, increment the iterator.
  • Compare the result value to a copy of the original number.
  • Print if the given number is a disarium number.
  • The Exit of the program.

Below is the implementation:

# Give the lower limit range as user input using the int(input()) function and store it in a variable.
lowlim_range = int(input("Enter some Random number = "))
# Give the upper limit range as user input using the int(input()) function and store it in another variable.
upplim_range = int(input("Enter some Random number = "))
print('The disarium numbers in the given range',
      lowlim_range, 'and', upplim_range, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for m in range(lowlim_range, upplim_range+1):

    # given number
    num = m
    # intialize result to zero(ans)
    ans = 0

    # calculating the digits
    digit_s = len(str(num))
    # copy the number in another variable(duplicate)
    dup_numbr = num
    while (dup_numbr != 0):
        # getting the last digit
        remaindr = dup_numbr % 10
        # multiply the result by a digit raised to the power of the iterator value.
        ans = ans + remaindr**digit_s
        digit_s = digit_s - 1
        dup_numbr = dup_numbr//10
    # It is disarium number if it is equal to original number
    if(num == ans):
        print(num, end=' ')

Output:

Enter some Random number = 1
Enter some Random number = 250
The disarium numbers in the given range 1 and 250 are:
1 2 3 4 5 6 7 8 9 89 135 175

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.