Python

Program to Get Tangent value Using math.tan()

Python Program to Get Tangent value Using math.tan()

In the previous article, we have discussed Python Program to Reverse each Word in a Sentence
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.tan() Function in Python:

Using this function, we can quickly find the tangent value for a given angle.

Tan is a trigonometric function that represents the Tangent function.
Its value can be calculated by dividing the length of the opposite side by the length of the adjacent side 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 tan() function in Python to obtain the tangent value for any given angle.

Examples:

Example1:

Input:

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

Output:

The Tangent values in a given range are tan( 0 ) =  0.0
The Tangent values in a given range are tan( 30 ) =  0.5773502691896257
The Tangent values in a given range are tan( 60 ) =  1.7320508075688767
The Tangent values in a given range are tan( 90 ) =  1.633123935319537e+16
The Tangent values in a given range are tan( 120 ) =  -1.7320508075688783
The Tangent values in a given range are tan( 150 ) =  -0.5773502691896257
The Tangent values in a given range are tan( 180 ) =  -1.2246467991473532e-16

Example 2:

Input:

Given lower limit range = 40
Given upper limit range = 300
Given step size = 45

Output:

The Tangent values in a given range are tan( 40 ) =  0.8390996311772799
The Tangent values in a given range are tan( 85 ) =  11.430052302761348
The Tangent values in a given range are tan( 130 ) =  -1.19175359259421
The Tangent values in a given range are tan( 175 ) =  -0.08748866352592402
The Tangent values in a given range are tan( 220 ) =  0.8390996311772799
The Tangent values in a given range are tan( 265 ) =  11.430052302761332

Program to Get Tangent value using math.tan()

Below are the ways to Get Tangent value.

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 Tangent value in a given range for the above-obtained radian values using math.tan() function and store it in another variable.
  • print the Tangent 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.
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 Tangent value in a given range of above obtained radian values using
    # tan()function and store it in another variable.
    tangt = math.tan(radns)
    # print the Tangent Values in the above given range.
    print("The Tangent values in a given range are tan(", vale, ') = ', tangt)

Output:

The Tangent values in a given range are tan( 0 ) =  0.0
The Tangent values in a given range are tan( 30 ) =  0.5773502691896257
The Tangent values in a given range are tan( 60 ) =  1.7320508075688767
The Tangent values in a given range are tan( 90 ) =  1.633123935319537e+16
The Tangent values in a given range are tan( 120 ) =  -1.7320508075688783
The Tangent values in a given range are tan( 150 ) =  -0.5773502691896257
The Tangent values in a given range are tan( 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 Tangent value in a given range of the above-obtained radian values using the tan()function and store it in another variable.
  • print the Tangent 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.
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 Tangent value in a given range of above obtained radian values using
    # tan()function and store it in another variable.
    tangt = math.tan(radns)
    # print the Tangent Values in the above given range.
    print("The Tangent values in a given range are tan(", vale, ') = ', tangt)

Output:

Enter some Random number = 40
Enter some Random number = 300
Enter some Random number = 45
The Tangent values in a given range are tan( 40 ) = 0.8390996311772799
The Tangent values in a given range are tan( 85 ) = 11.430052302761348
The Tangent values in a given range are tan( 130 ) = -1.19175359259421
The Tangent values in a given range are tan( 175 ) = -0.08748866352592402
The Tangent values in a given range are tan( 220 ) = 0.8390996311772799
The Tangent values in a given range are tan( 265 ) = 11.430052302761332

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.

 

Program to Find Super Factorial of a Number.

Python Program to Find Super Factorial of a Number

In the previous article, we have discussed Python Program to Calculate the Value of nPr
Super factorial :

Super factorial of a number is obtained by multiplying the first N factorials of a given Number.

Given a number, and the task is to find the superfactorial of an above-given number.

Examples:

Example1:

Input:

Given Number = 5

Output:

The super Factorial value of above given number =  34560

Example 2:

Input:

Given Number = 4

Output:

The super Factorial value of above given number =  288

Program to Find SuperFactorial of a Number.

Below are the ways to find SuperFactorial of a given Number.

Method #1: Using For loop (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Take a variable and initialize its value with ‘1’.
  • Loop from ‘1 ‘ to the above-given number using For loop.
  • Calculate the factorial of the iterator value using the built-in factorial method and multiply it with the above-initialized superfactorial value.
  • Store it in another variable.
  • Print the superfactorial value of the above-given number.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the number as static input and store it in a variable.
gvn_numbr = 5
# Take a variable and initialize it's value with '1'.
supr_factrl = 1
# Loop from '1 ' to above given number using For loop.
for iteror in range(gvn_numbr+1):
  # Calculate the factorial of the iterator value using built-in factorial method
  # and multiply it with above initialized superfactorial value.
 # Store it in another variable.
    supr_factrl = supr_factrl * math.factorial(iteror)
# Print the superfactorial value of the above given number.
print("The super Factorial value of above given number = ", supr_factrl)

Output:

The super Factorial value of above given number =  34560

Method #2: Using For Loop (User input)

Approach:

  • Import math module using the import keyword.
  • Give the number as User input and store it in a variable.
  • Take a variable and initialize its value with ‘1’.
  • Loop from ‘1 ‘ to the above-given number using For loop.
  • Calculate the factorial of the iterator value using the built-in factorial method and multiply it with the above-initialized superfactorial value.
  • Store it in another variable.
  • Print the superfactorial value of the above-given number.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the number as User input and store it in a variable.
gvn_numbr = int(input("Enter some Random Number ="))
# Take a variable and initialize it's value with '1'.
supr_factrl = 1
# Loop from '1 ' to above given number using For loop.
for iteror in range(gvn_numbr+1):
  # Calculate the factorial of the iterator value using built-in factorial method
  # and multiply it with above initialized superfactorial value.
 # Store it in another variable.
    supr_factrl = supr_factrl * math.factorial(iteror)
# Print the superfactorial value of the above given number.
print("The super Factorial value of above given number = ", supr_factrl)

Output:

Enter some Random Number = 4
The super Factorial value of above given number = 288

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.

Program to Print all Disarium Numbers within Given range

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

Program to Calculate the Value of nPr

Python Program to Calculate the Value of nPr

In the previous article, we have discussed Python Program to Find the Number of Weeks between two Dates
nPr:

nPr indicates n permutation r.

The process of organizing all the individuals in a given set to form a sequence is referred to as permutation.

The Given mathematical formula for nPr =n!/(n-r)!

Given the values of n, r and the task is to find the value of nPr .

Examples:

Example1:

Input:

Given n value = 5 
Given r value = 4

Output:

The value of nPr for above given n, r values =  120

Example 2:

Input:

Given n value = 6
Given r value = 3

Output:

The value of nPr for above given n, r values = 120

Program to Calculate the Value of nPr

Below are the ways to calculate the value of nPr for given values of n,r.

Method #1: Using math Module (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Give another number as static input and store it in another variable.
  • Calculate the value of nPr with reference to the standard mathematical formula n!/(n-r)! using factorial() function
  • Store it in a variable.
  • Print the value of nPr for the above-given n, r values.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the number as static input and store it in a variable.
gvn_n_val = 5
# Give another number as static input and store it in another variable.
gvn_r_val = 4
# Calculate the value of nPr with reference to standard mathematical formula n!/(n-r)!
# using factorial() function 
# Store it in a variable.
n_p_r = math.factorial(gvn_n_val)//math.factorial(gvn_n_val-gvn_r_val)
# Print the value of nPr for the above given n, r values.
print("The value of nPr for above given n, r values = ", n_p_r)

Output:

The value of nPr for above given n, r values =  120

Method #2: Using math Module (User input)

Approach:

  • Import math module using the import keyword.
  • Give the number as User input using the int(input()) function and store it in a variable.
  • Give another number as User input using the int(input()) function and store it in another variable.
  • Calculate the value of nPr with reference to the standard mathematical formula n!/(n-r)! using factorial() function
  • Store it in a variable.
  • Print the value of nPr for the above-given n, r values.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the number as user input using the int(input()) function and store it in a variable.
gvn_n_val = int(input("Enter some Random number = "))
# Give another number as user input using the int(input()) function and store it in another variable.
gvn_r_val = int(input("Enter some Random number = "))
# Calculate the value of nPr with reference to standard mathematical formula n!/(n-r)!
# using factorial() function 
# Store it in a variable.
n_p_r = math.factorial(gvn_n_val)//math.factorial(gvn_n_val-gvn_r_val)
# Print the value of nPr for the above given n, r values.
print("The value of nPr for above given n, r values = ", n_p_r)

Output:

Enter some Random number = 6
Enter some Random number = 3
The value of nPr for above given n, r values = 120

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.

Program To Calculate the Angle Between Two Vectors

Python Program To Calculate the Angle Between Two Vectors

In the previous article, we have discussed Python Program to Find the Sine Series for the Given range
Mathematical Way :

The angle between two vectors can be calculated using the formula, which states that the angle cos of two vectors is equal to the dot product of two vectors divided by the dot product of the mod of two vectors.

cosθ = X.Y/|X|.|Y|  =>θ =  cos^-1 X.Y/|X|.|Y|

Example:

Let  X={5,6} and y={4,3} are two vectors.

The angle between given two vectors = X.Y=  5*4+6*3 = 38

|X| = √ 5^2 +6^2 = 7.8102

|Y| = √ 4^2 +3^2 = 5

cosθ = 38/7.8102*5 = 0.973

θ= 13.324°

In this, we use the ‘math’ module to perform some complex calculations for us, such as square root, cos inverse, and degree, by calling the functions sqrt(), acos(), and degrees ().

Given two vectors, and the task is to calculate the angle between the given two vectors.

Examples:

Example1:

Input: 

Given x1, y1, x2, y2 = 5, 6, 4, 3

Output:

The Cos angle between given two vectors = 0.9730802874900094
The angle in degree between given two vectors =  13.324531261890783

Example 2:

Input:

Given x1, y1, x2, y2  =  7, 3, 2, 1

Output:

The Cos angle between given two vectors = 0.9982743731749958
The angle in degree between given two vectors = 3.36646066342994

Program To Calculate the Angle Between Two Vectors

Below are the ways to Calculate the Angle Between Two Vectors.

Method #1: Using Mathematical Formula (Static input)

Approach:

  • Import math module using the import keyword.
  • Give four variables as static input and store them in four different variables.
  • Calculate the dot product of the given two vectors using the above mathematical formula and store them in another variable.
  • Calculate the Mod of given two vectors using the above mathematical formula and store them in another variable.
  • Find the cosine angle between the given two vectors using the above mathematical formula and store them in another variable.
  • Print the cosine angle between the given two vectors.
  • Calculate the angle in degrees using built-in math.degrees(), math.acos() functions and store it in a variable .
  • Print the angle ‘θ’ between the given two vectors.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give four variables as static input and store them in four different variables.
x1, y1, x2, y2 = 5, 6, 4, 3


def angle_between_Gvnvectors(x1, y1, x2, y2):
    # Calculate the dot product of given two vectors using above mathematical formula
    # and store them in another variable.

    dot_Prodct = x1*x2 + y1*y2
 # Calculate the Mod of given two vectors using above mathematical formula and
# store them in another variable
    mod_Of_Vectr1 = math.sqrt(x1*x1 + y1*y1)*math.sqrt(x2*x2 + y2*y2)
 # Find the cosine angle between given two vectors  using above mathematical formula
# and store them in another variable.
    cosine_angle = dot_Prodct/mod_Of_Vectr1
 # Print the cosine angle between given two vectors.
    print(" The Cos angle between given two vectors =", cosine_angle)
# Calculate the angle in degrees using built-in math.degrees(), math.acos() functions
# and store it in a variable .
    angl_in_Degres = math.degrees(math.acos(cosine_angle))
# Print the angle 'θ' between given two vectors.
    print("The angle in degree between given two vectors = ", angl_in_Degres)


angle_between_Gvnvectors(x1, y1, x2, y2)

Output:

The Cos angle between given two vectors = 0.9730802874900094
The angle in degree between given two vectors =  13.324531261890783

Method #2: Using Mathematical Formula (User input)

Approach:

  • Import math module using the import keyword.
  • Give four variables as user input using map(),split() , int(input()) functions and store them in four different variables.
  • Calculate the dot product of the given two vectors using the above mathematical formula and store them in another variable.
  • Calculate the Mod of given two vectors using the above mathematical formula and store them in another variable.
  • Find the cosine angle between the given two vectors using the above mathematical formula and store them in another variable.
  • Print the cosine angle between the given two vectors.
  • Calculate the angle in degrees using built-in math.degrees(), math.acos() functions and store it in a variable .
  • Print the angle ‘θ’ between the given two vectors.
  • The Exit of the program.

Below is the implementation of the above approach:

# Import math module using the import keyword.
import math
# Give four variables as user input using map(),split() ,int(input())functions and 
#store them in four different variables.
x1, y1, x2, y2 = map(int,input("Enter four random numbers = ").split())
def angle_between_Gvnvectors(x1, y1, x2, y2):
    # Calculate the dot product of given two vectors using above mathematical formula
    # and store them in another variable.

    dot_Prodct = x1*x2 + y1*y2
 # Calculate the Mod of given two vectors using above mathematical formula and
# store them in another variable
    mod_Of_Vectr1 = math.sqrt(x1*x1 + y1*y1)*math.sqrt(x2*x2 + y2*y2)
 # Find the cosine angle between given two vectors  using above mathematical formula
# and store them in another variable.
    cosine_angle = dot_Prodct/mod_Of_Vectr1
 # Print the cosine angle between given two vectors.
    print(" The Cos angle between given two vectors =", cosine_angle)
# Calculate the angle in degrees using built-in math.degrees(), math.acos() functions
# and store it in a variable .
    angl_in_Degres = math.degrees(math.acos(cosine_angle))
# Print the angle 'θ' between given two vectors.
    print("The angle in degree between given two vectors = ", angl_in_Degres)

angle_between_Gvnvectors(x1, y1, x2, y2)

Output:

Enter four random numbers = 7 3 2 1
The Cos angle between given two vectors = 0.9982743731749958
The angle in degree between given two vectors = 3.36646066342994

Here we calculated the angle between the given vectors.

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.

Program to Find the Sine Series for the Given range

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

Program to Find Armstrong Number in an Interval

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

Program to Calculate EMI

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

Program to Find the Missing Term of any Arithmetic Progression

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

 

Program to Check Buzz Number or Not

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