Python Program to Check Buzz Number or Not

Program to Check Buzz Number or Not

In the previous article, we have discussed Python Program to Calculate the Area of a Trapezoid
Buzz Number:

If a number ends in 7 or is divisible by 7, it is referred to as a Buzz Number.

some of the examples of Buzz numbers are 14, 42, 97,107, 147, etc

The number 42 is a Buzz number because it is divisible by ‘7’.
Because it ends with a 7, the number 107 is a Buzz number.

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

Examples:

Example1:

Input:

Given number = 97

Output:

The given number { 97 } is a Buzz Number

Example2:

Input:

Given number = 120

Output:

The given number { 120 } is not a Buzz Number

Program to Check Buzz Number or Not

Below are the ways to check whether the given number is Buzz Number or not.

Method #1: Using modulus operator (Static input)

Approach:

  • Give the number as static input and store it in a variable.
  • Check if the given number modulus ‘7’ is equal to ‘0’ or if the given number modulus ’10’ is equal to ‘7’ or not using if conditional statement.
  • If the above statement is True, then print “The given number is a Buzz Number”.
  • Else if the statement is False, print “The given number is Not a Buzz Number”.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 14
# Check if the given number modulus '7' is equal to '0' or if the given number modulus '10'
# is  equal to '7' or not using if  conditional statement.
if gvn_numbr % 7 == 0 or gvn_numbr % 10 == 7:
 # If the above statement is True ,then print "The given number is a Buzz Number".
    print("The given number {", gvn_numbr, "} is a Buzz Number")
else:
  # Else if the statement is False, print "The given number is Not a Buzz Number" .
    print("The given number {", gvn_numbr, "} is not a Buzz Number")

Output:

The given number { 14 } is a Buzz Number

Here the number 14 is a Buzz Number.

Method #2: Using modulus operator (User input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • Check if the given number modulus ‘7’ is equal to ‘0’ or if the given number modulus ’10’ is equal to ‘7’ or not using if conditional statement.
  • If the above statement is True, then print “The given number is a Buzz Number”.
  • Else if the statement is False, print “The given number is Not a Buzz 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_numbr = int(input("Enter some random number = "))
# Check if the given number modulus '7' is equal to '0' or if the given number modulus '10'
# is  equal to '7' or not using if  conditional statement.
if gvn_numbr % 7 == 0 or gvn_numbr % 10 == 7:
 # If the above statement is True ,then print "The given number is a Buzz Number".
    print("The given number {", gvn_numbr, "} is a Buzz Number")
else:
  # Else if the statement is False, print "The given number is Not a Buzz Number" .
    print("The given number {", gvn_numbr, "} is not a Buzz Number")

Output:

Enter some random number = 49
The given number { 49 } is a Buzz Number

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 Common Characters between Two Strings

Program to Find Common Characters between Two Strings

In the previous article, we have discussed Python Program to Extract Only Characters from a Given String
Given two strings, the task is to find the common characters between Two Strings.

In this case, we use some of the built-in functions like join(), lower(), sorted() and intersection() methods.

join() :

The string method join()  takes all of the items in an iterable and returns a string after joining them all together. Iterable types include list, tuple, string, dictionary, and set.

lower() :

As the name indicates, It takes a string and converts all uppercase characters to lowercase before returning it.

sorted method() :

The sorted() method is used to orderly sort the elements of a sequence (either ascending or descending).

intersection() method :

The intersection() method is used to find all the elements that are shared by two given sets.

Examples:

Example 1:

Input:

Given First String = Hello Btechgeeks
Given Second String = good morning

Output:

The Common Characters between the above given two Strings =   go

Example 2:

Input:

Given First String = have a nice Day
Given Second String = A special day

Output:

The Common Characters between the above given two Strings = acdeiy

Program to Find Common Characters between Two Strings

Below are the ways to find common characters between two strings.

Method #1: Using intersection Method (Static Input)

Approach:

  • Give the first string as static input, convert the given string into the lower case using the built-in lower() method and store it in a variable.
  • Give the second string as static input, convert the given string into the lower case using the built-in lower() method and store it in another variable.
  • Get the Common characters between both the above-given strings using the built-in intersection() method which is a set method.
  • Sort the above-given string using the built-in sorted() method.
  • Join the above-given string using the built-in join()method.
  • Print all the Common Characters between the above given two Strings.
  • The Exit of the program.

Below is the implementation:

# Give the first string as static input  , convert the given string into lower case
# using built-in lower() method and store it in a variable.
fst_strng = "Hello Btechgeeks".lower()
# Give the  second string as static input , convert the given string into lower case
# using built-in lower() method and store it in another variable.
secnd_strng = "good morning".lower()
# Get the Common characters between both the above given strings using built-in
# intersection() method which is a set method.
# Sort the above given string using  built-in sorted() method.
# Join the the above given string using built-in join()method .
# Print all the Common Characters between the above given two Strings.
print("The Common Characters between the above given two Strings = ",
      ''.join(sorted(set.intersection(set(fst_strng), set(secnd_strng)))))

Output:

The Common Characters between the above given two Strings =   go

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

Approach:

  • Give the first string as User input using the input() function, convert the given string into the lower case using the built-in lower() method and store it in a variable.
  • Give the second string as User input using the input() function, convert the given string into the lower case using the built-in lower() method, and store it in another variable.
  • Get the Common characters between both the above-given strings using the built-in intersection() method which is a set method.
  • Sort the above-given string using the built-in sorted() method.
  • Join the above-given string using the built-in join()method.
  • Print all the Common Characters between the above given two Strings.
  • The Exit of the program.

Below is the implementation:

# Give the first string as User input  using the input() function , convert the given string into lower case
# using built-in lower() method and store it in a variable.
fst_strng = input("Enter some Random String = ").lower()
# Give the  second string as User input  using the input() function, convert the given string into lower case
# using built-in lower() method and store it in another variable.
secnd_strng = input("Enter some Random String = ").lower()
# Get the Common characters between both the above given strings using built-in
# intersection() method which is a set method.
# Sort the above given string using  built-in sorted() method.
# Join the the above given string using built-in join()method .
# Print all the Common Characters between the above given two Strings.
print("The Common Characters between the above given two Strings = ",
      ''.join(sorted(set.intersection(set(fst_strng), set(secnd_strng)))))

Output:

Enter some Random String = have a nice Day
Enter some Random String = A special day
The Common Characters between the above given two Strings = acdeiy

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 Sum of all the Factors of a Number

Program to Get Sum of all the Factors of a Number

In the previous article, we have discussed Python Program to Find Product of Values of elements in a Dictionary
Given a number, and the task is to get the sum of all the 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 all the factors of 24 = 1+2+ 3+4+6+ 8+12+24 = 60

Examples:

Example1:

Input:

Given Number = 24

Output:

The Sum of all the factors of { 24 } is =  60

Example 2:

Input:

Given Number = 140

Output:

The Sum of all the factors of { 140 } is =  336

Program to Get Sum of all the Factors of a Number

Below are the ways to get the sum of all the 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 a list and initialize it with ‘1’ and store it in another variable.
  • Loop from ‘2’  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, append the iterator value to the above-declared list.
  • Get the sum of all the factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the 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 a list and initialize it with '1' and store it in another variable.
all_factors = [1]
# Loop from '2' to above given number range using For loop.
for itr in range(2, 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 ,append the iterator value to the above declared list .
        all_factors.append(itr)
  # Get the sum of all the 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 the factors of a given number.
print("The Sum of all the factors of {", gvn_numb, "} is = ", reslt)

Output:

The Sum of all the factors of { 24 } is =  60

Method #2: Using For Loop (User Input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • Take a list and initialize it with ‘1’ and store it in another variable.
  • Loop from ‘2’  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, append the iterator value to the above-declared list.
  • Get the sum of all the factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the 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 a list and initialize it with '1' and store it in another variable.
all_factors = [1]
# Loop from '2' to above given number range using For loop.
for itr in range(2, 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 ,append the iterator value to the above declared list .
        all_factors.append(itr)
  # Get the sum of all the 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 the factors of a given number.
print("The Sum of all the factors of {", gvn_numb, "} is = ", reslt)

Output:

Enter some random number = 140
The Sum of all the factors of { 140 } is = 336

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 Check if a String is a keyword or Not

Program to Check if a String is a keyword or Not

In the previous article, we have discussed Python Program to Remove Elements from a Tuple
Definition of Keyword:–

A keyword is a reserved word in programming languages that has its own unique meaning. It conveys their unique meaning to the interpreter while executing. And, when using variables in code, we never use the keyword as the variable name.

Some of the keywords in Python are :

True, False, finally, not, or, and, if, else, elif, None, lambda, nonlocal, not, except, as, pass, try, def, in, with, while, import, continue, from, raise, return, global, class, break, from, assert, for, in, with, is, yield, del, and so on.

kwlist method:

To accomplish this, we must import a built-in Python module called “keyword,” and within the keyword module, there is a method called “kwlist” that stores all of the keywords found in the Python language in a list. And if the given string appears in the list, it is considered a keyword; otherwise, it is not a keyword.

Examples:

Example1:

Input:

Given First String = btechgeeks
Given Second String = for

Output:

The given string{ btechgeeks } is not a keyword
The given string{ for } is a keyword

Example 2:

Input:

Given First String = while
Given Second String = for

Output:

The given string{ while } is a keyword
The given string{ for } is a keyword

Program to Check if a String is a keyword or Not

Below are the ways to check if a String is a keyword or Not.

Method #1: Using kwlist Method (Static Input)

Approach:

  • Import keyword module using the import keyword.
  • Get all the keywords in python using keyword.kwlist method and store it in a variable.
  • Give the first string as static input and store it in another variable.
  • Give the second string as static input and store it in another variable.
  • Check whether the given first string is present in the above keyword list or not using the if conditional statement.
  • If the given condition is True, print “keyword”.
  • If the given condition is False, print ” Not keyword” using else conditional statement.
  • Similarly, Check whether the given second string is present in the above keyword or not list using if conditional statement.
  • If the given condition is True, print “keyword”.
  • If the given condition is False, print ” Not keyword” using else conditional statement.
  • The Exit of the Program.

Below is the implementation:

# Import keyword module using import keyword.
import keyword
# Get all the keywords in python using keyword.kwlist method and store it in a variable.
keywrds_lst = keyword.kwlist
# Give the first string as static input and store it in another variable.
fst_str = "btechgeeks"
# Give the second string as static input and store it in another variable.
secnd_str = "for"
# Check whether the given first string is present in the above keyword list or not
# using if conditional statement.
if fst_str in keywrds_lst:
  # If the given condition is True , print "keyword".
    print("The given string{", fst_str, "} is a keyword")
else:
  # If the given condition is False , print " Not keyword" using else conditional statement.
    print("The given string{", fst_str, "} is not a keyword")
# Check whether the given second string is present in the above keyword or not list
# using if conditional statement.
if secnd_str in keywrds_lst:
  # If the given condition is True , print "keyword".
    print("The given string{", secnd_str, "} is a keyword")
else:
  # If the given condition is False , print " Not keyword" using else conditional statement.
    print("The given string{", secnd_str, "} is not a keyword")

Output:

The given string{ btechgeeks } is not a keyword
The given string{ for } is a keyword

Method #2: Using kwlist Method (User Input)

Approach:

  • Import keyword module using the import keyword.
  • Get all the keywords in python using keyword.kwlist method and store it in a variable.
  • Give the first string as user input using the input() function and store it in another variable.
  • Give the second string as user input using the input() function and store it in another variable.
  • Check whether the given first string is present in the above keyword list or not using the if conditional statement.
  • If the given condition is True, print “keyword”.
  • If the given condition is False, print ” Not keyword” using else conditional statement.
  • Similarly, Check whether the given second string is present in the above keyword or not list using if conditional statement.
  • If the given condition is True, print “keyword”.
  • If the given condition is False, print ” Not keyword” using else conditional statement.
  • The Exit of the Program.

Below is the implementation:

# Import keyword module using import keyword.
import keyword
# Get all the keywords in python using keyword.kwlist method and store it in a variable.
keywrds_lst = keyword.kwlist
# Give the first string as user input using the input() function and
# store it in another variable.
fst_str = input("Enter some random string = ")
# Give the second string as user input using the input() function and
# store it another variable.
secnd_str = input("Enter some random string = ")
# Check whether the given first string is present in the above keyword list or not
# using if conditional statement.
if fst_str in keywrds_lst:
  # If the given condition is True , print "keyword".
    print("The given string{", fst_str, "} is a keyword")
else:
  # If the given condition is False , print " Not keyword" using else conditional statement.
    print("The given string{", fst_str, "} is not a keyword")
# Check whether the given second string is present in the above keyword or not list
# using if conditional statement.
if secnd_str in keywrds_lst:
  # If the given condition is True , print "keyword".
    print("The given string{", secnd_str, "} is a keyword")
else:
  # If the given condition is False , print " Not keyword" using else conditional statement.
    print("The given string{", secnd_str, "} is not a keyword")

Output:

Enter some random string = while
Enter some random string = for
The given string{ while } is a keyword
The given string{ for } is a keyword

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 Product of Values of elements in a Dictionary

Program to Find Product of Values of elements in a Dictionary

In the previous article, we have discussed Python Program to Check if a String is a keyword or Not
Dictionary in python :

A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.

example:

dict = {‘january’ :1, ‘febrauary’: 2, ‘march’: 3 }

Given a dictionary, and the task is to find the Product of values of elements in a dictionary.

Examples:

Example1:

Input:

Given dictionary = {'jan': 10, 'Feb': 5, 'Mar': 22, 'April': 32, 'May': 6}

Output:

The Product of values in a given dictionary =  211200

Example2:

Input: 

Given dictionary = {'a': 1, 'b': 5, 'c': 2, 'd': 4, 'e': 7, 'f': 2}

Output:

The Product of values in a given dictionary =  560

Program to Find Product of Values of elements in a Dictionary

Below are the ways to Find the Product of Values of elements in a Dictionary.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the dictionary by initializing it with some random values and store it in a variable.
  • Get all the values of the given dictionary using the dictionary.values() method and store it in another variable.
  • Take a variable to say ‘product’ and initialize its value with ‘1’
  • Iterate in the above-given dictionary values using For loop.
  • Inside the loop, Multiply the above-initialized product variable with the iterator and store it in the same variable.
  • Print the product of values for the above-given dictionary.
  • The Exit of the program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict = {'jan': 10, 'Feb': 5, 'Mar': 22, 'April': 32, 'May': 6}
# Get all the values of given dictionary using dictionary.values() method
# and store it in another variable.
dict_vlue = gvn_dict.values()
# Take a variable say 'product' and initialize it's value with '1'
fnl_prod = 1
# Iterate in the above given dictionary values using using For loop.
for itrator in dict_vlue:
  # Inside the loop, Multiply the above initialized product variable with the iterator
  # and store it in a same variable.
    fnl_prod = fnl_prod*itrator
# Print the product of values for the above given dictionary.
print("The Product of values in a given dictionary = ", fnl_prod)

Output:

The Product of values in a given dictionary =  211200

Method #2: Using For Loop (User Input)

Approach:

  • Take a dictionary and initialize it with an empty dictionary using dict() or {}.
  • Give the number of keys as user input using int(input()) and store it in a variable.
  • Loop till the given number of keys using for loop.
  • Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
  • Initialize the key with the value of the dictionary.
  • Get all the values of the given dictionary using the dictionary.values() method and store it in another variable.
  • Take a variable to say ‘product’ and initialize its value with ‘1’
  • Iterate in the above-given dictionary values using For loop.
  • Inside the loop, Multiply the above-initialized product variable with the iterator and store it in the same variable.
  • Print the product of values for the above-given dictionary.
  • The Exit of the program.

Below is the implementation:

# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dict = {}
# Give the number of keys as user input using int(input()) and store it in a variable.
numb_of_kys = int(
    input('Enter some random number of keys of the dictionary = '))
# Loop till the given number of keys using for loop.
for p in range(numb_of_kys):
        # Inside the for loop scan the key and value as
    # user input using input(),split() functions
    # and store them in two separate variables.
    keyy, valuee =  input(
        'Enter key and value separated by spaces = ').split()
    # Initialize the key with the value of the dictionary.
    gvn_dict[keyy] = valuee

# Get all the values of given dictionary using dictionary.values() method
# and store it in another variable.
dict_vlue = gvn_dict.values()
# Take a variable say 'product' and initialize it's value with '1'
fnl_prod = 1
# Iterate in the above given dictionary values using using For loop.
for itrator in dict_vlue:
  # Inside the loop, Multiply the above initialized product variable with the iterator
  # and store it in a same variable.
    fnl_prod = fnl_prod*int(itrator)
# Print the product of values for the above given dictionary.
print("The Product of values in a given dictionary = ", fnl_prod)

Output:

Enter some random number of keys of the dictionary = 5
Enter key and value separated by spaces = hello 4
Enter key and value separated by spaces = this 9
Enter key and value separated by spaces = is 10
Enter key and value separated by spaces = btechgeeks 12
Enter key and value separated by spaces = python 1
The Product of values in a given dictionary = 4320

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 Sine Series for the Given range

Program to Find the Sine Series for the Given range

In the previous article, we have discussed Python Program to Find Super Factorial of a Number.
Math Module :

Python’s math module is a built-in module. By importing this module, we can perform mathematical computations.

Numerous mathematical operations like ceil( ),floor( ),factorial( ),mod( ),value of pi ,…..etc .can be computed with the help of math module.

math.sin() function:

To compute the sine value, we must use the sine function as math. sine() takes only one parameter, the degree value.

Sin is a trigonometric function that represents the Sine function. Its value can be calculated by dividing the length of the opposite side by the length of the hypotenuse of a right-angled triangle, as we learned in math.

By importing the math module that contains the definition, we will be able to use the sin() function in Python to obtain the sine value for any given angle.

Examples:

Example1:

Input:

Given lower limit range =0
Given upper limit range =181
Given step size =30

Output:

The Sine values in a given range are : 
sin 0 = 0.0
sin 30 = 0.49999999999999994
sin 60 = 0.8660254037844386
sin 90 = 1.0
sin 120 = 0.8660254037844387
sin 150 = 0.49999999999999994
sin 180 = 1.2246467991473532e-16

Example 2:

Input:

Given lower limit range =  50
Given upper limit range = 200 
Given step size = 20

Output:

The Sine values in a given range are : 
sin 50 = 0.766044443118978
sin 70 = 0.9396926207859083
sin 90 = 1.0
sin 110 = 0.9396926207859084
sin 130 = 0.766044443118978
sin 150 = 0.49999999999999994
sin 170 = 0.17364817766693028
sin 190 = -0.17364817766693047

Program to Find the Sine Series for the Given range

Below are the ways to find the sine values in the Given Range.

Method #1: Using math Module (Static input)

Approach:

  • Import math module using the import keyword.
  • 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.
  • Give the step size as static input and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Get the angle value in radians from the given range(converting from degree to radians ) and store it in a variable.
  • Calculate the Sine value in a given range for the above-obtained radian values using math.sin() function and store it in another variable.
  • Print the Sine Values in the above-given range.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the lower limit range as static input and store it in a variable.
lwer_lmt = 0
# Give the upper limit range as static input and store it in another variable.
upp_lmt = 181
# Give the step size as static input and store it in another variable.
stp_sze = 30
# Loop from lower limit range to upper limit range using For loop.
print("The Sine values in a given range are : ")
for vale in range(lwer_lmt, upp_lmt, stp_sze):
  # Get the angle value in radians from the given range(converting from degree to radians )
  # and store it in a variable.
    radns = math.radians(vale)
    # Calculate the Sine value in a given range of above obtained radian values using
    # math.Sine()function and store it in another variable.
    sine_vlue = math.sin(radns)
    # print the Sine Values in the above given range.
    print("sin",vale, "=", sine_vlue)

Output:

The Sine values in a given range are : 
sin 0 = 0.0
sin 30 = 0.49999999999999994
sin 60 = 0.8660254037844386
sin 90 = 1.0
sin 120 = 0.8660254037844387
sin 150 = 0.49999999999999994
sin 180 = 1.2246467991473532e-16

Method #2: Using math Module (User input)

Approach:

  • Import math module using the import keyword.
  • 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.
  • Give the step size as User input and store it in another variable.
  • Loop from lower limit range to upper limit range using For loop.
  • Get the angle value in radians from the given range(converting from degree to radians ) and store it in a variable.
  • Calculate the Sine value in a given range for the above-obtained radian values using math.sin() function and store it in another variable.
  • print the Sine Values in the above-given range.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the lower limit range as User input using the int(input()) function and store it in a variable.
lwer_lmt = 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.
upp_lmt = int(input('Enter some Random number ='))
# Give the step size as User input and store it in another variable.
stp_sze = int(input('Enter some Random number ='))
# Loop from lower limit range to upper limit range using For loop.
print("The Sine values in a given range are : ")
for vale in range(lwer_lmt, upp_lmt, stp_sze):
  # Get the angle value in radians from the given range(converting from degree to radians )
  # and store it in a variable.
    radns = math.radians(vale)
    # Calculate the Sine value in a given range for the  above obtained radian values using
    # math.sin()function and store it in another variable.
    sine_vlue = math.sin(radns)
    # print the Sine Values in the above given range.
    print("sin",vale, "=", sine_vlue)

Output:

Enter some Random number = 50
Enter some Random number = 200
Enter some Random number = 20
The Sine values in a given range are : 
sin 50 = 0.766044443118978
sin 70 = 0.9396926207859083
sin 90 = 1.0
sin 110 = 0.9396926207859084
sin 130 = 0.766044443118978
sin 150 = 0.49999999999999994
sin 170 = 0.17364817766693028
sin 190 = -0.17364817766693047

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 Armstrong Number in an Interval

Program to Find Armstrong Number in an Interval

In the previous article, we have discussed Python Program To Calculate the Angle Between Two Vectors
Armstrong Number:

Beginners sometimes ask what the Armstrong number, also known as the narcissist number, is. Because of the way the number behaves in a given number base, it is particularly interesting to new programmers and those learning a new programming language. The Armstrong number meaning in numerical number theory is the number in any given number base that forms the sum of the same number when each of its digits is raised to the power of the number’s digits.

Ex: 153, 371 etc.

Explanation:

Here 1^3 + 5^3 + 3^3 = 153  so it is Armstrong Number

Example1:

Input:

Given lower limit =  50
Given upper limit =  1800

Output:

The Armstrong numbers in the given range: 
153
370
371
407
1634

Example 2:

Input:

Given lower limit =  500
Given upper limit =  2000

Output:

The Armstrong numbers in the given range: 
1634

Program to Find Armstrong Number in an Interval

Below are the ways to Find Armstrong numbers in a given interval.

Method #1: Using For Loop (Static input)

Approach:

  1. Give the lower limit range as static input and store it in a variable.
  2. Give the upper limit range as static input and store it in another variable.
  3. Loop from lower limit range to upper limit range using For loop.
  4. Inside the for loop take a variable say ‘num’ and initialize it’s value to  iterator value .
  5. Count how many digits there are in the number.
  6. Using mod and division operations, each digit is accessed one after the other.
  7. Each digit is multiplied by the number of digits, and the result is saved in a separate variable.
  8. Steps 6 and 7 are repeated until all of the digits have been used.
  9. Analyze the result obtained using the original number.
    • If both are same/equal then it is armstrong number
    • Else it is not armstrong number.
  10. Print the number if it is an Armstrong number in a Given range.
  11. The Exit of the Program.

Below is the implementation:

# Give the lower limit range as static input and store it in a variable.
gvn_lower_lmt = 50
# Give the upper limit range as static input and store it in another variable.
gvn_upper_lmt = 1800
# Loop from lower limit range to upper limit range using For loop.
print("The Armstrong numbers in the given range: ")
for numbr in range(gvn_lower_lmt, gvn_upper_lmt + 1):

    # Inside the for loop take a variable say 'num' and initialize it's value to
    # iterator value .
    # Count how many digits there are in the number.
    no_digits = len(str(numbr))

 # intialize result to zero(tot_sum)
    tot_sum = 0
  # copy the number in another variable(duplicate)
    tempry = numbr
    while tempry > 0:
      # getting the last digit
        rem = tempry % 10
       # multiply the result by a digit raised to the power of the number of digits
        tot_sum += rem ** no_digits
        tempry //= 10
   # It is Armstrong number if it is equal to original number
    if numbr == tot_sum:
        print(numbr)

Output:

The Armstrong numbers in the given range: 
153
370
371
407
1634

Method #2: Using For Loop (User input)

Approach:

  1. Give the lower limit range as user input and store it in a variable.
  2. Give the upper limit range as user input and store it in another variable.
  3. Loop from lower limit range to upper limit range using For loop.
  4. Inside the for loop take a variable say ‘num’ and initialize it’s value to  iterator value .
  5. Count how many digits there are in the number.
  6. Using mod and division operations, each digit is accessed one after the other.
  7. Each digit is multiplied by the number of digits, and the result is saved in a separate variable.
  8. Steps 6 and 7 are repeated until all of the digits have been used.
  9. Analyze the result obtained using the original number.
    • If both are same/equal then it is armstrong number
    • Else it is not armstrong number.
  10. Print the number if it is an Armstrong number in a Given range.
  11. The Exit of the Program.

Below is the implementation:

# Give the lower limit range as user input and store it in a variable.
gvn_lower_lmt = int(input("Enter some Random number = "))
# Give the upper limit range as user 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 Armstrong numbers in the given range: ")
for numbr in range(gvn_lower_lmt, gvn_upper_lmt + 1):

    # Inside the for loop take a variable say 'num' and initialize it's value to
    # iterator value .
    # Count how many digits there are in the number.
    no_digits = len(str(numbr))

 # intialize result to zero(tot_sum)
    tot_sum = 0
  # copy the number in another variable(duplicate)
    tempry = numbr
    while tempry > 0:
      # getting the last digit
        rem = tempry % 10
       # multiply the result by a digit raised to the power of the number of digits
        tot_sum += rem ** no_digits
        tempry //= 10
   # It is Armstrong number if it is equal to original number
    if numbr == tot_sum:
        print(numbr)

Output:

Enter some Random number = 500
Enter some Random number = 2000
The Armstrong numbers in the given range: 
1634

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 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.