Vikram Chiluka

Program to Check if all Characters of String are Alphanumeric or Not

Python Program to Check if all Characters of String are Alphanumeric or Not

In the previous article, we have discussed Python Program to Calculate EMI

isalnum() Method:

The isalnum() Method is a String Class pre-defined method that can be used on any String Object. It returns Boolean Values based on the following criteria.

  • If all of the characters in the given string are Alphanumerical, it returns True.
  • If any of the characters in the given string are not Alphanumerical, this function returns False.

For Example:

Given string = “Hellobtechgeeks123”

Output: returns true since all the characters are alphabets and numbers.

Given string = “good morning@ #btechgeeks12345”

Output: returns False since all the given String contains some special symbols like #,@, and spaces.

Examples:

Example1:

Input:

Given string = "Hellobtechgeeks123"

Output:

The Given string { Hellobtechgeeks123 } is Alpha Numeric

Example 2:

Input:

Given string = "good morning@ #btechgeeks12345"

Output:

The Given string { good morning@ #btechgeeks12345 } is not Alpha Numeric

Program to Check if all Characters of String are Alphanumeric or Not

Below are the ways to Check if all Characters of String are Alphanumeric or Not.

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

Approach:

  • Give the string as static input and store it in a variable.
  • Check whether the Given string is alphanumeric using the built-in alnum() method and store it in another variable.
  • Check whether the above result is True or not using the if conditional statement.
  • If it is True, Print the given string is alphanumeric.
  • If it is False, Print the given string that is not alphanumeric.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng = "Hellobtechgeeks123"
# Check whether the Given string is alphanumeric using built-in alnum() method
# and store it in another variable.
Bol_val = gvn_strng.isalnum()
# Check whether the above result is True or not using the if conditional statement.
if(Bol_val == True):
 # If it is True, Print the given string is  alphanumeric.
    print("The Given string {", gvn_strng, "} is Alpha Numeric")
else:
  # If it is False, Print the given string is not alphanumeric.
    print("The Given string {", gvn_strng, "} is not Alpha Numeric")

Output:

The Given string { Hellobtechgeeks123 } is Alpha Numeric

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

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Check whether the Given string is alphanumeric using the built-in alnum() method and store it in another variable.
  • Check whether the above result is True or not using the if conditional statement.
  • If it is True, Print the given string is alphanumeric.
  • If it is False, Print the given string that is not alphanumeric.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input()function and store it in a variable.
gvn_strng = input('Enter some random string = ')
# Check whether the Given string is alphanumeric using built-in alnum() method
# and store it in another variable.
Bol_val = gvn_strng.isalnum()
# Check whether the above result is True or not using the if conditional statement.
if(Bol_val == True):
 # If it is True, Print the given string is  alphanumeric.
    print("The Given string {", gvn_strng, "} is Alpha Numeric")
else:
  # If it is False, Print the given string is not alphanumeric.
    print("The Given string {", gvn_strng, "} is not Alpha Numeric")

Output:

Enter some random string = good morning @btechgeeks #123456
The Given string { good morning @btechgeeks #123456 } is not Alpha Numeric

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

Python Program to Check if a String is Lapindrome or Not

In the previous article, we have discussed Python Program to Calculate BMI
Lapindrome:

When strings are divided into halves, if the partitioned strings have the same frequency (i.e. character count) on both partitions, those strings are referred to as lapindromes. If there are an odd number of characters, ignore the middle character.

Example:

Given String = pqrsprq

When the given string is divided into two halves we get pqr and prq (ignore middle character). Both these strings have the same characters. Hence the given String is Lapindrome.

Examples:

Example 1:

Input:

Given String = pqrsprq

Output:

Yes,the above Given string is a lapindrome

Example 2:

Input:

Given String = btechgeeks

Output:

No, the above given string is not a lapindrome

Program to Check if a String is lapindrome or Not

Below are the ways to check if the given  String is lapindrome or Not

Method #1: Using Slicing Operator (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Initialize two empty stings(S1, S2).
  • If the length of the Given String is even, then divide it into two equal halves using the slicing operator and append it to the above two declared empty strings.
  • Else if the length of the Given String is odd, then divide it into two equal halves using the slicing operator(Ignore middle character ) and append it to the above two declared empty strings.
  • Convert the split strings S1, S2 into two lists respectively using the built-in list() function and store them in another variable.
  • Sort the above two lists in ascending order using the sort() function and store them in another variable.
  • Convert the above two lists into two different strings respectively and store them in two variables.
  • If both the given split strings are equal, print “Lapindrome ” using the if conditional statement.
  • print “Not Lapindrome”, if both the given split strings are not equal.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_strng = "pqrsprq"
# Initialize two empty stings
fst_strng, secnd_strng = '', ''
# If the length of the Given String is even ,then divide it into two equal halves
# using slicing operator and append it to the above two declared empty strings.
if(len(gvn_strng) % 2 == 0):
    fst_strng = gvn_strng[:len(gvn_strng)//2]
    secnd_strng = gvn_strng[len(gvn_strng)//2:]
# Else if the length of the Given String is odd ,then divide it into two equal halves
# using slicing operator(Ignore middle character ) and append it to the above two declared empty strings.
else:
    fst_strng = gvn_strng[:len(gvn_strng)//2]
    secnd_strng = gvn_strng[len(gvn_strng)//2+1:]
# Convert the split strings S1, S2 into two lists respectively using built-in list()
# function and store them in another variables.
lst_1 = list(fst_strng)
lst_2 = list(secnd_strng)
# Sort the above two lists in ascending order and store them in another variables .
lst_1.sort()
lst_2.sort()
# Convert the above two lists into two different strings respectively and
# store them in another variables .
fst_strng = str(lst_1)
secnd_strng = str(lst_2)
# If both the given split strings are equal ,print "Lapindrome " using if condition.
if(fst_strng == secnd_strng):
    print('Yes,the above Given string is a lapindrome')
# print "Not Lapindrome", if both the given split strings  are not equal .
else:
    print('No, the above given string is not a lapindrome')

Output:

Yes,the above Given string is a lapindrome

Method #2: Using Slicing Operator (User Input)

Approach:

  • Give the string as User input using the input() function and store it in a variable.
  • Initialize two empty stings(S1, S2).
  • If the length of the Given String is even, then divide it into two equal halves using the slicing operator and append it to the above two declared empty strings.
  • Else if the length of the Given String is odd, then divide it into two equal halves using the slicing operator(Ignore middle character ) and append it to the above two declared empty strings.
  • Convert the split strings S1, S2 into two lists respectively using the built-in list() function and store them in another variable.
  • Sort the above two lists in ascending order using the sort() function and store them in another variable.
  • Convert the above two lists into two different strings respectively and store them in two variables.
  • If both the given split strings are equal, print “Lapindrome ” using the if condition.
  • print “Not Lapindrome”, if both the given split strings are not equal.
  • The exit of the program.

Below is the implementation:

# Give the string as User input and store it in a variable.
gvn_strng = input("Enter Some Random String = ")
# Initialize two empty stings
fst_strng, secnd_strng = '', ''
# If the length of the Given String is even ,then divide it into two equal halves
# using slicing operator and append it to the above two declared empty strings.
if(len(gvn_strng) % 2 == 0):
    fst_strng = gvn_strng[:len(gvn_strng)//2]
    secnd_strng = gvn_strng[len(gvn_strng)//2:]
# Else if the length of the Given String is odd ,then divide it into two equal halves
# using slicing operator(Ignore middle character ) and append it to the above two declared empty strings.
else:
    fst_strng = gvn_strng[:len(gvn_strng)//2]
    secnd_strng = gvn_strng[len(gvn_strng)//2+1:]
# Convert the split strings S1, S2 into two lists respectively using built-in list()
# function and store them in another variables.
lst_1 = list(fst_strng)
lst_2 = list(secnd_strng)
# Sort the above two lists in ascending order and store them in another variables .
lst_1.sort()
lst_2.sort()
# Convert the above two lists into two different strings respectively and
# store them in another variables .
fst_strng = str(lst_1)
secnd_strng = str(lst_2)
# If both the given split strings are equal ,print "Lapindrome " using if condition.
if(fst_strng == secnd_strng):
    print('Yes, the above Given string is a lapindrome')
# print "Not Lapindrome", if both the given split strings  are not equal .
else:
    print('No, the above given string is not a lapindrome')

Output:

Enter Some Random String = btechgeeks
No, the above given string is not a lapindrome

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 Pronic Number or Not

Python Program to Check Pronic Number or Not

In the previous article, we have discussed Python Program to Calculate the Discriminant Value
Pronic Number:

A pronic number is the product of two consecutive integers, i.e. a number of the form n(n + 1).

Pronic numbers are also referred to as oblong numbers or heteromecic numbers.

Pronic numbers include 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, and 462 etc.

Examples:

Example 1:

Input:

Given number = 240

Output: 

The given number{ 240 } is a pronic number

Example 2:

Input:

Given number = 15

Output: 

The given number{ 15 } is Not a pronic number

Program to Check Pronic Number or Not

Below are the ways to check the given number is a pronic number or not

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take a variable say ‘count’ and initialize its value with ‘0’.
  • Loop from 0 to above-given number using for loop.
  • Inside the loop, check if the product of the iterator value and iterator+1 value (consecutive integers) is equal to the given number using the if conditional statement.
  • If the statement is true, then increment the value of count (i.e. 1), give the break condition, and come out of the loop.
  • Check if the value of count is equal to ‘1’ using the if conditional statement.
  • If the statement is true, then print “The given number is pronic number”.
  • Else print “The given number is Not a pronic number”.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numb = 156
# Take a variable say 'count' and initialize its value with '0'.
count = 0
# Loop from 0 to above-given number using for loop. 
for itr in range(numb):
    # Inside the loop, check if the product of the iterator value and iterator+1 value
    # (consecutive integers) is equal to the given number using the if conditional statement.
    if itr * (itr + 1) == numb:
        # If the statement is true, then increment the value of count (i.e. 1),
        # give the break condition and come out of the loop.
        count = 1
        break
# Check if the value of count is equal to '1' using the if conditional statement.
if count == 1:
  # If the statement is true, then print "The given number is a pronic number".
    print("The given number{",numb,"} is a pronic number")
# Else print "The given number is Not a pronic number".
else:
    print("The given number{",numb,"} is Not a pronic number")

Output: 

The given number{ 156 } is a pronic number

Method #2: Using For Loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Take a variable say ‘count’ and initialize its value with ‘0’.
  • Loop from 0 to above-given number using for loop.
  • Inside the loop, check if the product of the iterator value and iterator+1 value (consecutive integers) is equal to the given number using the if conditional statement.
  • If the statement is true, then increment the value of count (i.e. 1), give the break condition, and come out of the loop.
  • Check if the value of count is equal to ‘1’ using the if conditional statement.
  • If the statement is true, then print “The given number is pronic number”.
  • Else print “The given number is Not a pronic number”.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using int(input()) function and store it in a variable.
numb = int(input("Enter some random number = "))
# Take a variable say 'count' and initialize its value with '0'.
count = 0
# Loop from 0 to above-given number using for loop. 
for itr in range(numb):
    # Inside the loop, check if the product of the iterator value and iterator+1 value
    # (consecutive integers) is equal to the given number using the if conditional statement.
    if itr * (itr + 1) == numb:
        # If the statement is true, then increment the value of count (i.e. 1),
        # give the break condition and come out of the loop.
        count = 1
        break
# Check if the value of count is equal to '1' using the if conditional statement.
if count == 1:
  # If the statement is true, then print "The given number is a pronic number".
    print("The given number{",numb,"} is a pronic number")
# Else print "The given number is Not a pronic number".
else:
    print("The given number{",numb,"} is Not a pronic number")

Output: 

Enter some random number = 90
The given number{ 90 } is a pronic 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.

 

Program to Calculate Surface Area and Volume of a Cylinder

Python Program to Calculate Surface Area and Volume of a Cylinder

In the previous article, we have discussed Python Program to Check Pronic Number or Not
Cylinder :

A cylinder is a closed geometrical solid shape with two parallel bases joined by a curved surface.
The bases are circular in shape.

The following is the formula for calculating

  1. The area of a cylinder = 2Ï€r(r+h)
  2. The volume of a cylinder = πr²h

where  r = radius of the base circle

h = height of the curved surface

Given the radius and height of the cylinder, and the task is to calculate the surface area and volume of the given cylinder.

Examples:

Example 1:

Input: 

Given radius = 2.5
Given height = 3.5

Output:

The surface area of a given cylinder = 137.44467859455347
The volume of a given cylinder = 68.72233929727673

Example 2:

Input: 

Given radius = 7
Given height = 1.5

Output:

The surface area of a given cylinder = 461.81412007769956
The volume of a given cylinder = 230.90706003884978

Program to Calculate Surface Area and Volume of a Cylinder

Below are the ways to calculate the surface area and volume of the given cylinder.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the radius as static input and store it in a variable.
  • Give the height as static input and store it in another variable.
  • Calculate the surface area of the given cylinder using the above given mathematical formula, math.pi () method and store it in another variable.
  • Calculate the volume of the given cylinder using the above given mathematical formula, math.pi () method and store it in another variable.
  • Print the surface area of the given cylinder.
  • Print the volume of the given cylinder.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the radius as static input and store it in a variable.
gvn_radius = 4
# Give the height as static input and store it in another variable.
gvn_heigt = 7
# Calculate the surface area of the given cylinder using the above given mathematical
# formula, math.pi () method and store it in another variable.
surfc_area = 2*math.pi*pow(gvn_radius, 2)*gvn_heigt
# Calculate the volume of the given cylinder using the above given mathematical formula,
# math.pi () method and store it in another variable.
vol = math.pi*pow(gvn_radius, 2)*gvn_heigt
# Print the surface area of the given cylinder.
print("The surface area of a given cylinder =", surfc_area)
# Print the volume of the given cylinder.
print("The volume of a given cylinder =", vol)

Output:

The surface area of a given cylinder = 703.7167544041137
The volume of a given cylinder = 351.85837720205683

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the radius as user input using the float(input()) function and store it in a variable.
  • Give the height as user input using the float(input()) function and store it in another variable.
  • Calculate the surface area of the given cylinder using the above given mathematical formula, math.pi () method and store it in another variable.
  • Calculate the volume of the given cylinder using the above given mathematical formula, math.pi () method and store it in another variable.
  • Print the surface area of the given cylinder.
  • Print the volume of the given cylinder.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the radius as user input using the float(input())function and store it in a variable.
gvn_radius = float(input("Enter some random number = "))
# Give the height as user input using the float(input())function store it in another variable.
gvn_heigt = float(input("Enter some random number = "))
# Calculate the surface area of the given cylinder using the above given mathematical
# formula, math.pi () method and store it in another variable.
surfc_area = 2*math.pi*pow(gvn_radius, 2)*gvn_heigt
# Calculate the volume of the given cylinder using the above given mathematical formula,
# math.pi () method and store it in another variable.
vol = math.pi*pow(gvn_radius, 2)*gvn_heigt
# Print the surface area of the given cylinder.
print("The surface area of a given cylinder =", surfc_area)
# Print the volume of the given cylinder.
print("The volume of a given cylinder =", vol)

Output:

Enter some random number = 5.5
Enter some random number = 8
The surface area of a given cylinder = 1520.53084433746
The volume of a given cylinder = 760.26542216873

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 out the Arc Length of an Angle

Python Program to Find out the Arc Length of an Angle

In the previous article, we have discussed Python Program to Calculate Surface Area and Volume of a Cylinder
Arc Length :

The length of an arc is defined as the length of a circle’s circumference.

The arc length formula is as follows:

Arc length = (Ï€*d) * (A/360)

where ‘d ‘ is the diameter of the circle.

‘A’ is the angle.

Given the diameter and angle of the circle, and the task is to find the arc length of the given angle.

Examples:

Example 1:

Input:

Given Diameter  = 10
Given Angle = 100

Output: 

The arc length of the given angle = 8.727

Example 2:

Input:

Given Diameter = 15
Given Angle =  450

Output: 

The above Entered Angle is Invalid

Program to Find out the Arc Length of an Angle

Below are the ways to find the arc length of the given angle.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the diameter of the circle as static input and store it in a variable.
  • Give the angle as static input and store it in another variable.
  • Check if the given angle is greater than or equal to 360 using the if conditional statement.
  • If the statement is true, then print “The above-Entered Angle is Invalid”.
  • Else calculate the Arc length of the given angle using the above mathematical formula, math. pi () method and store it in another variable.
  • Print the arc length of the given angle.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the diameter of the circle as static input and store it in a variable.
gvn_diametr = 10
# Give the angle as static input and store it in another variable.
gvn_Angle = 100
# Check if the given angle is greater than or equal to 360 using if conditional statement.
if gvn_Angle >= 360:
  # If the statement is true, then print "The above Entered Angle is Invalid".
    print("The above Entered Angle is Invalid")
else:
    # Else Calculate the Arc length of the given angle using the above mathematical formula,
    # math.pi() method and store it in another variable.
    Arc_len = (math.pi*gvn_diametr) * (gvn_Angle/360)
# Print the arc length of the given angle.
    print("The arc length of the given angle = %.3f" % Arc_len)

Output: 

The arc length of the given angle = 8.727

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the diameter of the circle as user input using the float(input()) function and store it in a variable.
  • Give the angle as user input using the float(input()) function and store it in another variable.
  • Check if the given angle is greater than or equal to 360 using the if conditional statement.
  • If the statement is true, then print “The above-Entered Angle is Invalid”.
  • Else Calculate the Arc length of the given angle using the above mathematical formula, math.pi () method and store it in another variable.
  • Print the arc length of the given angle.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the diameter of the circle as user input using the float(input()) function and
# store it in a variable.
gvn_diametr = float(input("Enter some random number = "))
# Give the angle as user input using the float(input()) function and
# store it in another variable.
gvn_Angle = float(input("Enter some random number = "))
# Check if the given angle is greater than or equal to 360 using if conditional statement.
if gvn_Angle >= 360:
  # If the statement is true, then print "The above Entered Angle is Invalid".
    print("The above Entered Angle is Invalid")
else:
# Else Calculate the Arc length of the given angle using the above mathematical formula,
# math.pi() method and store it in another variable.
    Arc_len = (math.pi*gvn_diametr) * (gvn_Angle/360)
# Print the arc length of the given angle.
    print("The arc length of the given angle = %.3f" % Arc_len)

Output: 

Enter some random number = 15
Enter some random number = 90
The arc length of the given angle = 11.781

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 Area of a Trapezoid

Python Program to Calculate the Area of a Trapezoid

In the previous article, we have discussed Python Program to Check Sunny Number
Trapezoid

A trapezoid is a four-sided geometrical figure with two sides that are parallel to each other. the sides that are parallel to each other are referred to as the “base” The other sides are referred to as “legs” (which may or may not be equal).

The formula for calculating the area of a trapezoid = (a+b)*h/2

where a, b = base of the trapezoid

h = denotes the distance between two parallel lines.

Given the base_1, base_2, and height of the trapezoid, and the task is to calculate the area of the given trapezoid.

Examples:

Example 1:

Input: 

Given first base = 6
Given second base = 2
Given height = 1

Output:

The Area of the given Trapezoid =  4.0

Example 2:

Input: 

Given first base =  10
Given second base = 9
Given height = 8.5

Output:

The Area of the given Trapezoid =  80.75

Program to Calculate the Area of a Trapezoid

Below are the ways to calculate the area of the given trapezoid.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the first base as static input and store it in a variable.
  • Give the second base as static input and store it in another variable.
  • Give the height of the given trapezoid as static input and store it in another variable.
  • Calculate the area of the given trapezoid using the above given mathematical formula and store it in another variable.
  • Print the area of the given trapezoid.
  • The Exit of the program.

Below is the implementation:

# Give the first base as static input and store it in a variable.
fst_base = 2
# Give the second base as static input and store it in another variable.
secnd_base = 4
# Give the height of the given trapezoid as static input and store it in another variable
gvn_Heigt = 1.5
# Calculate the area of the given trapezoid using the above given mathematical formula
# and store it in another variable.
trapezod_area = 0.5 * (fst_base + secnd_base) * gvn_Heigt
print("The Area of the given Trapezoid = ", trapezod_area)

Output:

The Area of the given Trapezoid =  4.5

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the first base as user input using the float(input()) function and store it in a variable.
  • Give the second base as user input using the float(input()) function and store it in another variable.
  • Give the height of the given trapezoid as user input using the float(input()) function and store it in another variable.
  • Calculate the area of the given trapezoid using the above given mathematical formula and store it in another variable.
  • Print the area of the given trapezoid.
  • The Exit of the program.

Below is the implementation:

# Give the first base as user input using the float(input()) function and
# store it in a variable.
fst_base = float(input("Enter some random number = "))
# Give the second base as user input using the float(input()) function and
# store it in another variable.
secnd_base = float(input("Enter some random number = "))
# Give the height of the given trapezoid as user input using the float(input()) function
# and store it in another variable.
gvn_Heigt = float(input("Enter some random number = "))
# Calculate the area of the given trapezoid using the above given mathematical formula
# and store it in another variable.
trapezod_area = 0.5 * (fst_base + secnd_base) * gvn_Heigt
print("The Area of the given Trapezoid = ", trapezod_area)

Output:

Enter some random number = 3
Enter some random number = 4
Enter some random number = 5
The Area of the given Trapezoid = 17.5

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.

 

Program to Check Sunny Number

Python Program to Check Sunny Number

In the previous article, we have discussed Python Program to Find the Missing Term of any Arithmetic Progression
Sunny Number:

A number is said to be a sunny number if the number next to it is a perfect square. In other words, if N+1 is a perfect square, then N is a sunny number.

Example :

Let Given number(N) = 3

Then N+1= 3+1 = 4 # which is a perfect square.

Therefore ,the Given number “3” is a Sunny Number.

some of the examples are 3,8 ,15, 24, 35 ,48 ,63, 80 ,99, 120 etc.

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

Examples:

Example1:

Input:

Given number = 3

Output:

The given number{ 3 } is Sunny Number

Example2:

Input:

Given number = 122

Output:

The given number{ 122 } is not a Sunny Number

Program to Check Sunny Number

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

Method #1: Using math.sqrt() function (Static input)

Approach:

  1. Import the math module using the import keyword.
  2. Give the number as static input and store it in a variable.
  3. Add ‘1’ to the above-given number and store it in another variable say “incremented number”.
  4. Calculate the Square root of the above-obtained number using math.sqrt() built-in function and store it in another variable.
  5. Multiply the above obtained Square root value with itself and store it in another variable say “square_number”.
  6. Check if the value of “square_number ” is equal to the “incremented number” using if conditional statement.
  7. If the statement is True,Print “The given number is Strong Number”
  8. Else if the statement is False, print “The given number is Not a Strong Number”.
  9. The Exit of the program.

Below is the implementation:

# Import the math module using import keyword.
import math
# Give the number as static input and store it in a variable.
numb = 3
# Add '1' to the above given number and store it in another variable say
# "incremented number".
incremnt_num = numb + 1
# Calculate the Square root of the above obtained number using math.sqrt()
# built-in function and store it in another variable.
sqrt_numb = math.sqrt(incremnt_num)
# Multiply the above obtained Square root value with itself and
# store it in another variable say "square_number".
square_number = sqrt_numb * sqrt_numb
# Check if the value of "square_number " is equal the "incremented number"
# using if conditional statement.
if(square_number == incremnt_num):
  # If the statement is True ,Print "The given number is Strong Number"
    print("The given number{", numb, "} is Sunny Number")
else:
  # Else if the statement is False, print "The given number is Not a Strong Number" .
    print("The given number{", numb, "} is not a Sunny Number")

Output:

The given number{ 3 } is Sunny Number

Method #2: Using math.sqrt() function (User input)

Approach:

  1. Import the math module using the import keyword.
  2. Give the number as user input using the int(input()) function and store it in a variable.
  3. Add ‘1’ to the above-given number and store it in another variable say “incremented number”.
  4. Calculate the Square root of the above-obtained number using math.sqrt() built-in function and store it in another variable.
  5. Multiply the above obtained Square root value with itself and store it in another variable say “square_number”.
  6. Check if the value of “square_number ” is equal to the “incremented number” using if conditional statement.
  7. If the statement is True, Print “The given number is Strong Number”
  8. Else if the statement is False, print “The given number is Not a Strong Number”.
  9. The Exit of the program.

Below is the implementation:

# Import the math module using import keyword.
import math
# Give the number as user input using int(input()) and store it in a variable.
numb = int(input("Enter some random number = "))
# Add '1' to the above given number and store it in another variable say
# "incremented number".
incremnt_num = numb + 1
# Calculate the Square root of the above obtained number using math.sqrt()
# built-in function and store it in another variable.
sqrt_numb = math.sqrt(incremnt_num)
# Multiply the above obtained Square root value with itself and
# store it in another variable say "square_number".
square_number = sqrt_numb * sqrt_numb
# Check if the value of "square_number " is equal the "incremented number"
# using if conditional statement.
if(square_number == incremnt_num):
  # If the statement is True ,Print "The given number is Strong Number"
    print("The given number{", numb, "} is Sunny Number")
else:
  # Else if the statement is False, print "The given number is Not a Strong Number" .
    print("The given number{", numb, "} is not a Sunny Number")

Output:

Enter some random number = 30
The given number{ 30 } is not a Sunny 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.

 

Program to Generate Strong Numbers in an Interval

Python Program to Generate Strong Numbers in an Interval

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

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

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

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

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

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

Examples:

Example 1:

Input:

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

Output:

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

Example 2:

Input:

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

Output:

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

Program to Generate Strong Numbers in an Interval

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

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

Approach:

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

Below is the implementation:

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

Output:

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

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

Approach:

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

Below is the implementation:

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

Output:

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

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