Author name: Vikram Chiluka

Program to Find Sum of Series 12-23+34-45+56...+NN+1

Python Program to Find Sum of Series 1/2-2/3+3/4-4/5+5/6…+N/N+1

In the previous article, we have discussed Python Program to Find Sum of Series (1+(1*2)+(1*2*3)+…till N)

Given a number N and the task is to find the sum of the given series (1/2-2/3+3/4-4/5+5/6…+N/N+1) for the given number in Python.

Examples:

Example1:

Input:

Given Number = 17

Output:

The total sum of the series till the given number { 17 } =  0.6661398242280596

Example2:

Input:

Given Number = 6

Output:

The total sum of the series till the given number { 6 } =  -0.2404761904761904

Program to Find Sum of Series 1/2-2/3+3/4-4/5+5/6…+N/N+1 in Python

Below are the ways to find the sum of the given series (1/2-2/3+3/4-4/5+5/6…+N/N+1) for the given number in Python :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.0 (floating number).
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Check if the value k modulus 2 is even or not using the if conditional statement.
  • If the statement is True, then subtract k/(k+1) from the above-declared resltsum.
  • Store it in the same variable resltsum.
  • Else if the statement is False, then add k/(k+1) to the above-declared resltsum.
  • Store it in the same variable resltsum.
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 17
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.0 (floating number).
resltsum = 0.0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
    # Check if the value k modulus 2 is even or not using the if conditional statement.
    if(k % 2 == 0):
        # If the statement is True, then subtract k/(k+1) from the above-declared resltsum.
        # Store it in the same variable resltsum.
        resltsum -= k/(k+1)
    else:
      # Else if the statement is False, then add k/(k+1) to the above-declared resltsum.
       # Store it in the same variable resltsum.
        resltsum += k/(k+1)
 # Increment the value of k by 1 and store it in the same variable k.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 17 } =  0.6661398242280596

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.0 (floating number).
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Check if the value k modulus 2 is even or not using the if conditional statement.
  • If the statement is True, then subtract k/(k+1) from the above-declared resltsum.
  • Store it in the same variable resltsum.
  • Else if the statement is False, then add k/(k+1) to the above-declared resltsum.
  • Store it in the same variable resltsum.
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.0 (floating number).
resltsum = 0.0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
    # Check if the value k modulus 2 is even or not using the if conditional statement.
    if(k % 2 == 0):
        # If the statement is True, then subtract k/(k+1) from the above-declared resltsum.
        # Store it in the same variable resltsum.
        resltsum -= k/(k+1)
    else:
      # Else if the statement is False, then add k/(k+1) to the above-declared resltsum.
       # Store it in the same variable resltsum.
        resltsum += k/(k+1)
 # Increment the value of k by 1 and store it in the same variable k.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some Random Number = 6
The total sum of the series till the given number { 6 } = -0.2404761904761904

Practice Python Program Examples to master coding skills and learn the fundamental concepts in the dynamic programming language Python.

 

Python Program to Find Sum of Series 1/2-2/3+3/4-4/5+5/6…+N/N+1 Read More »

Program to Find sum of series (1+(12)+(123)+...till N)

Python Program to Find Sum of Series (1+(1*2)+(1*2*3)+…till N)

In the previous article, we have discussed Python Program to Find Sum of Series (1+(1+2)+(1+2+3)+…till N)
Given a number N and the task is to find the sum of the given series (1+(1*2)+(1*2*3)+…till N) for the given number in Python.

Examples:

Example1:

Input:

Given Number = 13

Output:

The total sum of the series till the given number { 13 } = 6749977113

Example2:

Input:

Given Number = 7

Output:

The total sum of the series till the given number { 7 } =  5913

Program to Find the sum of series (1+(1*2)+(1*2*3)+…till N) in Python

Below are the ways to find the sum of the given series (1+(1*2)+(1*2*3)+…till N) for the given number in Python :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop take a variable say gvn_multiplr and initialize its value with 1.
  • Iterate from 1 to the value of k using the for loop.
  • Multiply the iterator value to the above-initialized gvn_multiplr and store it in the same variable gvn_multiplr.
  • Add the above-obtained value of gvn_multiplr to the resltsum outside the for loop.
  • Store it in the same variable resltsum.
  • Increment the value of k by 1 outside the for loop.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input.
# store it in a variable.
gvn_numb = 13
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
    # Inside the loop take a variable say gvn_multiplr and initialize its value with 1.
    gvn_multiplr = 1
  # Iterate from 1 to the value of k using the for loop.
    for itr in range(1, k+1):
      # Multiply the iterator value to the above-initialized gvn_multiplr and store it in the same
        # variable gvn_multiplr.
        gvn_multiplr *= itr
 # Add the above-obtained value of gvn_multiplr to the resltsum outside the for loop.
# Store it in the same variable resltsum.
    resltsum += gvn_multiplr
  # Increment the value of k by 1 outside the for loop.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 13 } =  6749977113

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop take a variable say gvn_multiplr and initialize its value with 1.
  • Iterate from 1 to the value of k using the for loop.
  • Multiply the iterator value to the above-initialized gvn_multiplr and store it in the same variable gvn_multiplr.
  • Add the above-obtained value of gvn_multiplr to the resltsum outside the for loop.
  • Store it in the same variable resltsum.
  • Increment the value of k by 1 outside the for loop.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
    # Inside the loop take a variable say gvn_multiplr and initialize its value with 1.
    gvn_multiplr = 1
  # Iterate from 1 to the value of k using the for loop.
    for itr in range(1, k+1):
      # Multiply the iterator value to the above-initialized gvn_multiplr and store it in the same
        # variable gvn_multiplr.
        gvn_multiplr *= itr
 # Add the above-obtained value of gvn_multiplr to the resltsum outside the for loop.
# Store it in the same variable resltsum.
    resltsum += gvn_multiplr
  # Increment the value of k by 1 outside the for loop.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some Random Number = 7
The total sum of the series till the given number { 7 } = 5913

If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.

Python Program to Find Sum of Series (1+(1*2)+(1*2*3)+…till N) Read More »

Program to find the Sum of Series 1^2+2^2+3^2...+N^2

Python Program to find the Sum of Series 1^2+2^2+3^2…+N^2

In the previous article, we have discussed Python Program to Convert Binary to Octal using While Loop
Given a number N the task is to find the sum of the given series 1^2+2^2+3^2…+N^2 for the given number in Python.

Examples:

Example1:

Input:

Given Number =3

Output:

The sum of the series till the given number { 3 } is : 14

Example2:

Input:

Given Number =5

Output:

The sum of the series till the given number { 6 } is : 55

Program to find the Sum of Series 1^2+2^2+3^2…+N^2 in Python

Below are the ways to find the sum of the given series 1^2+2^2+3^2…+N^2 for the given number in Python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 1 to given number using the For loop.
  • Inside the For loop, Calculate the value of iterator value * iterator value and store it in a variable.
  • Add the above variable to the resltsum .
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvnNumbr = 5
# Take a variable say resltsum which gives the sum of
# the given series till N and initialize its value to 0.
resltsum = 0
# Loop from 1 to given number using the For loop.
for k in range(1, gvnNumbr+1):
    # Inside the For loop,
    # Calculate the value of iterator value * iterator value and store it in a variable.
    squarevalu = k*k
    # Add the above variable to the resltsum
    resltsum = resltsum+squarevalu

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

The sum of the series till the given number { 5 } is : 55

Time Complexity: O(N) where N is the value of the given number (As the loop runs N times)

Method #2: Using For loop and pow() function (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 1 to given number using the For loop.
  • Inside the For loop, Calculate the value of iterator value * iterator value and store it in a variable.
  • Add the above variable to the resltsum .
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function
# and store it in a variable.
gvnNumbr = int(input('Enter some random number N = '))
# Take a variable say resltsum which gives the sum of
# the given series till N and initialize its value to 0.
resltsum = 0
# Loop from 1 to given number using the For loop.
for k in range(1, gvnNumbr+1):
    # Inside the For loop,
    # Calculate the value of iterator value * iterator value and store it in a variable.
    squarevalu = k*k
    # Add the above variable to the resltsum
    resltsum = resltsum+squarevalu

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

Enter some random number N = 3
The sum of the series till the given number { 3 } is : 14

Time Complexity: O(N) where N is the value of the given number (As the loop runs N times)

Method #3: Using Mathematical Formula (Static Input)

We can calculate the sum of the above series directly by using the mathematical formula :

(n * ( n + 1 ) *( 2 * n +1) ) / 6

Approach:

  • Give the number N as static input and store it in a variable.
  • Calculate the sum of the given series using the above mathematical formula and store it in a variable to say resltsum.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvnNumbr = 5
# Calculate the sum of the given series using the above mathematical formula
# and store it in a variable to say resltsum
resltsum = (gvnNumbr*(gvnNumbr+1)*((2*gvnNumbr)+1))//6

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

The sum of the series till the given number { 5 } is : 55

Time Complexity: O(1)

Method #4: Using For loop and ** operator (User Input)

We can calculate the sum of the above series directly by using the mathematical formula :

(n * ( n + 1 ) *( 2 * n +1) ) / 6

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Calculate the sum of the given series using the above mathematical formula and store it in a variable to say resltsum.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function
# and store it in a variable.
gvnNumbr = int(input('Enter some random number N = '))
# Calculate the sum of the given series using the above mathematical formula
# and store it in a variable to say resltsum
resltsum = (gvnNumbr*(gvnNumbr+1)*((2*gvnNumbr)+1))//6

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

Enter some random number N = 8
The sum of the series till the given number { 8 } is : 204

Time Complexity: O(1)

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

Python Program to find the Sum of Series 1^2+2^2+3^2…+N^2 Read More »

Program to find the Sum of Series 1^1+2^2+3^3...+N^N

Python Program to find the Sum of Series 1^1+2^2+3^3…+N^N

In the previous article, we have discussed Python Program to Convert Octal to Hexadecimal
Given a number N the task is to find the sum of the given series 1^1+2^2+3^3+…..+N^N for the given number in Python.

Examples:

Example1:

Input:

Given Number =3

Output:

The sum of the series till the given number { 3 } is : 32

Example2:

Input:

Given Number =6

Output:

The sum of the series till the given number { 6 } is : 50069

Program to find the Sum of Series 1^1+2^2+3^3…+N^N in Python

Below are the ways to find the sum of the given series 1^1+2^2+3^3+…..+N^N for the given number in Python:

Method #1: Using For loop and pow() function (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 1 to given number using the For loop.
  • Inside the For loop, Calculate the value of iterator value ^ iterator value using the pow function and store it in a variable.
  • Add the above variable to the resltsum .
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvnNumbr = 3
# Take a variable say resltsum which gives the sum of
# the given series till N and initialize its value to 0.
resltsum = 0
# Loop from 1 to given number using the For loop.
for k in range(1, gvnNumbr+1):
        # Inside the For loop, Calculate the value of iterator value ^ iterator value
    # using the pow function and store it in a variable.
    powervl = pow(k, k)
    # Add the above variable to the resltsum
    resltsum = resltsum+powervl

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

The sum of the series till the given number { 3 } is : 32

Method #2: Using For loop and pow() function (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 1 to given number using the For loop.
  • Inside the For loop, Calculate the value of iterator value ^ iterator value using the pow function and store it in a variable.
  • Add the above variable to the resltsum .
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function
# and store it in a variable.
gvnNumbr = int(input('Enter some random number N = '))
# Take a variable say resltsum which gives the sum of
# the given series till N and initialize its value to 0.
resltsum = 0
# Loop from 1 to given number using the For loop.
for k in range(1, gvnNumbr+1):
        # Inside the For loop, Calculate the value of iterator value ^ iterator value
    # using the pow function and store it in a variable.
    powervl = pow(k, k)
    # Add the above variable to the resltsum
    resltsum = resltsum+powervl

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

Enter some random number N = 7
The sum of the series till the given number { 7 } is : 873612

Method #3: Using For loop and ** operator (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 1 to given number using the For loop.
  • Inside the For loop, Calculate the value of iterator value ^ iterator value using the ** operator and store it in a variable.
  • Add the above variable to the resltsum .
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvnNumbr = 5
# Take a variable say resltsum which gives the sum of
# the given series till N and initialize its value to 0.
resltsum = 0
# Loop from 1 to given number using the For loop.
for k in range(1, gvnNumbr+1):
    # Inside the For loop, Calculate the value of iterator value ^ iterator value
    # using the ** operator
    # and store it in a variable.
    powervl = k**k
    # Add the above variable to the resltsum
    resltsum = resltsum+powervl

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

The sum of the series till the given number { 5 } is : 3413

Method #4: Using For loop and ** operator (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Loop from 1 to given number using the For loop.
  • Inside the For loop, Calculate the value of iterator value ^ iterator value using the ** operator and store it in a variable.
  • Add the above variable to the resltsum .
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function
# and store it in a variable.
gvnNumbr = int(input('Enter some random number N = '))
# Take a variable say resltsum which gives the sum of
# the given series till N and initialize its value to 0.
resltsum = 0
# Loop from 1 to given number using the For loop.
for k in range(1, gvnNumbr+1):
    # Inside the For loop, Calculate the value of iterator value ^ iterator value
    # using the ** operator
    # and store it in a variable.
    powervl = k**k
    # Add the above variable to the resltsum
    resltsum = resltsum+powervl

# Print the resltsum value which is the result of the series till the given Number N.
print(
    'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)

Output:

Enter some random number N = 6
The sum of the series till the given number { 6 } is : 50069

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

Python Program to find the Sum of Series 1^1+2^2+3^3…+N^N Read More »

Python Program to Read and Display a Matrix

In the previous article, we have discussed Python Program to find the Sum of Series 1^1+2^2+3^3…+N^N
Given a matrix, the task is to scan and print the elements of the given matrix in Python

What is a matrix:

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

Program to Read and Display a Matrix in Python

Below are the ways to read and display the elements of the given matrix in Python:

Method #1: Using list Method

Approach:

  • Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
  • Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
  • Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
  • Loop till the given number of rows using the For loop
  • Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
  • Add the above row elements list to gvnmatrix using the append() function.
  • To print all the elements of the given matrix.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
  • Print the element of the matrix by printing gvnmatrix[n][m] value where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • The Exit of the Program.

Below is the Implementation:

# Give the number of rows of the matrix as user input using the int(input()) function
# and store it in a variable.
mtrxrows = int(input('Enter some random number of rows of the matrix = '))
# Give the number of columns of the matrix as user input using the int(input()) function
# and store it in another variable.
mtrxcols = int(input('Enter some random number of columns of the matrix = '))
# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
mtrx = []
# Loop till the given number of rows using the For loop
for n in range(mtrxrows):
    # Inside the For loop, Give all the row elements of the given Matrix as a list using
    # the list(),map(),int(),split() functions and store it in a variable.
    l = list(map(int, input(
        'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))
    # Add the above row elements list to gvnmatrix using the append() function.

    mtrx.append(l)

# To print all the elements of the given matrix.
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
        # Inside the For loop, Iterate till the given number of rows using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Print the element of the matrix by printing gvnmatrix[n][m] value
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        print(mtrx[n][m], end=' ')
    print()

Output:

Enter some random number of rows of the matrix = 3
Enter some random number of columns of the matrix = 4
Enter {4} elements of row {1} separated by spaces = 5 8 3 7
Enter {4} elements of row {2} separated by spaces = 5 9 7 2
Enter {4} elements of row {3} separated by spaces = 1 6 8 5
5 8 3 7 
5 9 7 2 
1 6 8 5

Method #2: Using List Comprehension

Approach:

  • Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
  • Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
  • Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
  • Loop till the given number of rows using the For loop
  • Inside the For loop, Give all the row elements of the given Matrix as a list using the list Comprehension.
  • Add the above row elements list to gvnmatrix using the append() function.
  • To print all the elements of the given matrix.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
  • Print the element of the matrix by printing gvnmatrix[n][m] value where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
  • The Exit of the Program.

Below is the Implementation:

# Give the number of rows of the matrix as user input using the int(input()) function
# and store it in a variable.
mtrxrows = int(input('Enter some random number of rows of the matrix = '))
# Give the number of columns of the matrix as user input using the int(input()) function
# and store it in another variable.
mtrxcols = int(input('Enter some random number of columns of the matrix = '))
# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
mtrx = []
# Loop till the given number of rows using the For loop
for n in range(mtrxrows):
    # Inside the For loop, Give all the row elements of the given Matrix as a list using the list Comprehension
    l=[int(eleme) for eleme in input('Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()]
    # Add the above row elements list to gvnmatrix using the append() function.
    mtrx.append(l)

# To print all the elements of the given matrix.
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
        # Inside the For loop, Iterate till the given number of rows using another
    # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Print the element of the matrix by printing gvnmatrix[n][m] value
        # where n is the iterator value of the parent For loop and m is the iterator
        # value of the inner For loop.
        print(mtrx[n][m],end= ' ')
    print()

Output:

Enter some random number of rows of the matrix = 4
Enter some random number of columns of the matrix = 5
Enter {5} elements of row {1} separated by spaces = 1 2 3 4 5
Enter {5} elements of row {2} separated by spaces = 7 8 5 9 4
Enter {5} elements of row {3} separated by spaces = 1 11 25 7 6
Enter {5} elements of row {4} separated by spaces = 2 5 3 4 6
1 2 3 4 5 
7 8 5 9 4 
1 11 25 7 6 
2 5 3 4 6

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

Python Program to Read and Display a Matrix Read More »

Program to Convert Octal to Hexadecimal

Python Program to Convert Octal to Hexadecimal

In the previous article, we have discussed Python Program to Convert Decimal to Hexadecimal
Given an octal number and the task is to get the respective hexadecimal number.

Examples:

Example1:

Input:

Given Octal number = 24

Output:

The Hexadecimal value of the given octal number { 24 } is: 
14

Example2:

Input:

Given Octal number = 72

Output:

The Hexadecimal value of the given octal number { 72 } is: 
3A

Program to Convert Octal to Hexadecimal in Python

Below are the ways to convert the given octal number into hexadecimal :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the octal number as static input and store it in a variable.
  • Take a variable say ‘k‘ and initialize its value with 0.
  • Take a list of length 50 and initialize all the values to zero using the multiplication operator.
  • Store it in another variable.
  • Take another variable say ‘deciml_num‘ and initialize its value with 0.
  • Take another variable say ‘tempry‘ and initialize its value with 0.
  • Loop till the given octal number is not equal to zero using the while loop.
  • Inside the loop, calculate the value of the given octal number modulus 10 and store it in another variable ‘a’.
  • Multiply ‘a’ with the 8 raised to the power tempry using the pow() function and store it in another variable ‘b’.
  • Add ‘b’ with the deciml_num and store it in the same variable deciml_num.
  • Increment the value of tempry by 1 and store it in the same variable tempry.
  • Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Calculate the value of deciml_num modulus 16 to get the remainder and store it in a variable.
  • Check if the above remainder value is less than 10 using the if conditional statement.
  • If the statement is true, then get the character with the ASCII value remainder+48 using the if conditional statement and store it in a variable
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Else if the statement is false, get the character with the ASCII value remainder+55.
  • Store it in another variable.
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Divide the decimal number by 16 and store it in the same variable deciml_num.
  • Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
  • Print the element present at the iterator of the hexadecimal list to get the hexadecimal value of the given octal number.
  • The Exit of the Program.

Below is the implementation:

# Give the octal number as static input and store it in a variable.
gvn_octl_num = 24
# Take a variable say 'k' and initialize its value with 0.
k = 0
# Take a list of length 50 and initialize all the values to zero using the multiplication
# operator.
# Store it in another variable.
Hexa_deciml = ['0']*50
# Take another variable say 'deciml_num' and initialize its value with 0.
deciml_num = 0
# Take another variable say 'tempry' and initialize its value with 0.
tempry = 0
print(
    "The Hexadecimal value of the given octal number {", gvn_octl_num, "} is: ")
# Loop till the given octal number is not equal to zero using the while loop.
while gvn_octl_num != 0:
  # Inside the loop, calculate the value of the given octal number modulus 10 and store
    # it in another variable 'a'.
    a = (gvn_octl_num % 10)
 # Multiply 'a' with the 8 raised to the power tempry using the pow() function and store
# it in another variable 'b'.
    b = pow(8, tempry)*a
  # Add 'b' with the deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num+b
 # Increment the value of tempry by 1 and store it in the same variable tempry.
    tempry += 1
  # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
    gvn_octl_num = gvn_octl_num // 10
# Loop till the decimal number is not equal to zero using the while loop.
while deciml_num != 0:
  # Calculate the value of deciml_num modulus 16 to get the remainder and store it in a
  # variable.
    remindr = deciml_num % 16
 # Check if the above remainder value is less than 10 using the if conditional statement.
    if remindr < 10:
        # If the statement is true, then get the character with the ASCII value remainder+48
        # using the if conditional statement and store it in a variable
        chrvalue = chr(remindr+48)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
 # Increment the value of k by 1 and store it in the same variable k.
        k += 1
    else:
     # Else if the statement is false, get the character with the ASCII value remainder+55.
        # Store it in another variable.
        chrvalue = chr(remindr+55)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
  # Increment the value of k by 1 and store it in the same variable k.
        k += 1
  # Divide the decimal number by 16 and store it in the same variable deciml_num.
    deciml_num //= 16
# Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
for itr in range(k-1, -1, -1):
  # Print the element present at the iterator of the hexadecimal list to get the
    # hexadecimal value of the given octal number.
    print(Hexa_deciml[itr], end='')

Output:

The Hexadecimal value of the given octal number { 24 } is: 
14

Method #2: Using While loop (User Input)

Approach:

  • Give the octal number as user input using the int(input()) function input and store it in a variable.
  • Take a variable say ‘k‘ and initialize its value with 0.
  • Take a list of length 50 and initialize all the values to zero using the multiplication operator.
  • Store it in another variable.
  • Take another variable say ‘deciml_num‘ and initialize its value with 0.
  • Take another variable say ‘tempry‘ and initialize its value with 0.
  • Loop till the given octal number is not equal to zero using the while loop.
  • Inside the loop, calculate the value of the given octal number modulus 10 and store it in another variable ‘a’.
  • Multiply ‘a’ with the 8 raised to the power tempry using the pow() function and store it in another variable ‘b’.
  • Add ‘b’ with the deciml_num and store it in the same variable deciml_num.
  • Increment the value of tempry by 1 and store it in the same variable tempry.
  • Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Calculate the value of deciml_num modulus 16 to get the remainder and store it in a variable.
  • Check if the above remainder value is less than 10 using the if conditional statement.
  • If the statement is true, then get the character with the ASCII value remainder+48 using the if conditional statement and store it in a variable
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Else if the statement is false, get the character with the ASCII value remainder+55.
  • Store it in another variable.
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Divide the decimal number by 16 and store it in the same variable deciml_num.
  • Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
  • Print the element present at the iterator of the hexadecimal list to get the hexadecimal value of the given octal number.
  • The Exit of the Program.

Below is the implementation:

# Give the octal number as user input using the int(input()) function input and
# store it in a variable.
gvn_octl_num = int(input("Enter some random number = "))
# Take a variable say 'k' and initialize its value with 0.
k = 0
# Take a list of length 50 and initialize all the values to zero using the multiplication
# operator.
# Store it in another variable.
Hexa_deciml = ['0']*50
# Take another variable say 'deciml_num' and initialize its value with 0.
deciml_num = 0
# Take another variable say 'tempry' and initialize its value with 0.
tempry = 0
print(
    "The Hexadecimal value of the given octal number {", gvn_octl_num, "} is: ")
# Loop till the given octal number is not equal to zero using the while loop.
while gvn_octl_num != 0:
  # Inside the loop, calculate the value of the given octal number modulus 10 and store
    # it in another variable 'a'.
    a = (gvn_octl_num % 10)
 # Multiply 'a' with the 8 raised to the power tempry using the pow() function and store
# it in another variable 'b'.
    b = pow(8, tempry)*a
  # Add 'b' with the deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num+b
 # Increment the value of tempry by 1 and store it in the same variable tempry.
    tempry += 1
  # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
    gvn_octl_num = gvn_octl_num // 10
# Loop till the decimal number is not equal to zero using the while loop.
while deciml_num != 0:
  # Calculate the value of deciml_num modulus 16 to get the remainder and store it in a
  # variable.
    remindr = deciml_num % 16
 # Check if the above remainder value is less than 10 using the if conditional statement.
    if remindr < 10:
        # If the statement is true, then get the character with the ASCII value remainder+48
        # using the if conditional statement and store it in a variable
        chrvalue = chr(remindr+48)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
 # Increment the value of k by 1 and store it in the same variable k.
        k += 1
    else:
     # Else if the statement is false, get the character with the ASCII value remainder+55.
        # Store it in another variable.
        chrvalue = chr(remindr+55)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
  # Increment the value of k by 1 and store it in the same variable k.
        k += 1
  # Divide the decimal number by 16 and store it in the same variable deciml_num.
    deciml_num //= 16
# Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
for itr in range(k-1, -1, -1):
  # Print the element present at the iterator of the hexadecimal list to get the
    # hexadecimal value of the given octal number.
    print(Hexa_deciml[itr], end='')

Output:

Enter some random number = 72
The Hexadecimal value of the given octal number { 72 } is: 
3A

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

Python Program to Convert Octal to Hexadecimal Read More »

Program to Convert Decimal to Hexadecimal

Python Program to Convert Decimal to Hexadecimal

In the previous article, we have discussed Python Program to Convert Hexadecimal To Octal
Given a decimal number and the task is to get the respective hexadecimal number using the while loop.

Examples:

Example1:

Input:

Given decimal number = 30

Output:

The Hexadecimal value of the given decimal number { 30 } is: 
1E

Example2:

Input:

Given decimal number = 60

Output:

The Hexadecimal value of the given decimal number { 60 } is: 
3C

Program to Convert Decimal to Hexadecimal in Python

Below are the ways to convert the given decimal number into hexadecimal :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the decimal number as static input and store it in a variable.
  • Take a variable say ‘k‘ and initialize its value with 0.
  • Take a list of length 50 and initialize all the values to zero using the multiplication operator.
  • Store it in another variable.
  • Loop till the given decimal number is not equal to zero using the while loop.
  • Inside the loop, calculate the value of decimal number modulus 16 to get the remainder and store it in another variable.
  • Check if the above remainder value is less than 10 using the if conditional statement.
  • If the statement is true, then get the character with the ASCII value remainder+48 using the if conditional statement and store it in a variable
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Else if the statement is false, get the character with the ASCII value remainder+55.
  • Store it in another variable.
  • Increment the value of k by 1 and store it in the same variable k.
  • Divide the given decimal number by 16 and store it in the same variable.
  • Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
  • Print the element present at the iterator of the hexadecimal list to get the hexadecimal value of the given decimal number.
  • The Exit of the Program.

Below is the implementation:

# Give the decimal number as static input and store it in a variable.
deciml_num = 30
# Take a variable say 'k' and initialize its value with 0.
k = 0
# Take a list of length 50 and initialize all the values to zero using the multiplication operator. 
# Store it in another variable.
Hexa_deciml = ['0']*50

print(
    "The Hexadecimal value of the given decimal number {", deciml_num, "} is: ")
# Loop till the given decimal number is not equal to zero using the while loop.
while deciml_num != 0:
 # Inside the loop, calculate the value of decimal number modulus 16 to get the remainder
    # and store it in another variable.
    remndr = deciml_num % 16
  # Check if the above remainder value is less than 10 using the if conditional statement.
    if remndr < 10:
       # If the statement is true, then get the character with the ASCII value remainder+48 using
        # the if conditional statement and store it in a variable
        chrvalue = chr(remndr+48)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
    # Increment the value of k by 1 and store it in the same variable k.
        k += 1

    else:
      # Else if the statement is false, get the character with the ASCII value remainder+55.
        # Store it in another variable.
        Hexa_deciml[k] = chr(remndr+55)
  # Increment the value of k by 1 and store it in the same variable k.
        k += 1
  # Divide the given decimal number by 16 and store it in the same variable.
    deciml_num //= 16
# Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
for itr in range(k-1, -1, -1):
  # Print the element present at the iterator value of the hexadecimal list to get the
    # hexadecimal value of the given decimal number.
    print(Hexa_deciml[itr], end="")

Output:

The Hexadecimal value of the given decimal number { 30 } is: 
1E

Method #2: Using While loop (User Input)

Approach:

  • Give the decimal number as user input using the int(input()) function input and store it in a variable.
  • Take a variable say ‘k‘ and initialize its value with 0.
  • Take a list of length 50 and initialize all the values to zero using the multiplication operator.
  • Store it in another variable.
  • Loop till the given decimal number is not equal to zero using the while loop.
  • Inside the loop, calculate the value of decimal number modulus 16 to get the remainder and store it in another variable.
  • Check if the above remainder value is less than 10 using the if conditional statement.
  • If the statement is true, then get the character with the ASCII value remainder+48 using the if conditional statement and store it in a variable
  • Initialize the hexadecimal list at the index ‘k’ with the above-calculated character.
  • Increment the value of k by 1 and store it in the same variable k.
  • Else if the statement is false, get the character with the ASCII value remainder+55.
  • Store it in another variable.
  • Increment the value of k by 1 and store it in the same variable k.
  • Divide the given decimal number by 16 and store it in the same variable.
  • Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
  • Print the element present at the iterator of the hexadecimal list to get the hexadecimal value of the given decimal number.
  • The Exit of the Program.

Below is the implementation:

# Give the decimal number as user input using the int(input()) function input and
# store it in a variable.
deciml_num = int(input("Enter some random Number = "))
# Take a variable say 'k' and initialize its value with 0.
k = 0
# Take a list of length 50 and initialize all the values to zero using the multiplication operator. 
# Store it in another variable.
Hexa_deciml = ['0']*50

print(
    "The Hexadecimal value of the given decimal number {", deciml_num, "} is: ")
# Loop till the given decimal number is not equal to zero using the while loop.
while deciml_num != 0:
 # Inside the loop, calculate the value of decimal number modulus 16 to get the remainder
    # and store it in another variable.
    remndr = deciml_num % 16
  # Check if the above remainder value is less than 10 using the if conditional statement.
    if remndr < 10:
       # If the statement is true, then get the character with the ASCII value remainder+48 using
        # the if conditional statement and store it in a variable
        chrvalue = chr(remndr+48)
  # Initialize the hexadecimal list at the index 'k' with the above-calculated character.
        Hexa_deciml[k] = chrvalue
    # Increment the value of k by 1 and store it in the same variable k.
        k += 1

    else:
      # Else if the statement is false, get the character with the ASCII value remainder+55.
        # Store it in another variable.
        Hexa_deciml[k] = chr(remndr+55)
  # Increment the value of k by 1 and store it in the same variable k.
        k += 1
  # Divide the given decimal number by 16 and store it in the same variable.
    deciml_num //= 16
# Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.
for itr in range(k-1, -1, -1):
  # Print the element present at the iterator value of the hexadecimal list to get the
    # hexadecimal value of the given decimal number.
    print(Hexa_deciml[itr], end="")

Output:

Enter some random Number = 60
The Hexadecimal value of the given decimal number { 60 } is: 
3C

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

Python Program to Convert Decimal to Hexadecimal Read More »

Program to Convert Hexadecimal To Octal

Python Program to Convert Hexadecimal To Octal

In the previous article, we have discussed Python Program to Convert Hexadecimal to Decimal
Given a hexadecimal number and the task is to get the respective octal number.

Examples:

Example1:

Input:

Given Hexadecimal String = "5B"

Output:

The Octal value of the given Hexadecimal number { 5B } is: 
133

Example2:

Input:

Given Hexadecimal String = "3DA"

Output:

The Octal value of the given Hexadecimal number { 3DA } is: 
1732

Program to Convert Hexadecimal To Octal in Python

Below are the ways to convert the given hexadecimal number into octal :

Method #1: Using For Loop (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the hexadecimal string as static input and store it in a variable.
  • Take a variable say ‘rslt_val ‘and initialize its value with 0.
  • Take another variable say deciml_num and initialize its value with 0.
  • Calculate the length of the given hexadecimal number and store it in a variable k.
  • Decrement the value of k by 1 and store it in the same variable k.
  • Loop from 0 to the length of the given hexadecimal number using the for loop.
  • Inside the loop, check if the iterator value of the hexadecimal string is greater than or equal to 0 and less than or equal to 9 using the if conditional statement.
  • If it is true, then convert the iterator value of the hexadecimal string into an integer using the int() function.
  • Assign its value to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘A’ or ‘a’ using the if conditional statement.
  • If it is true, then assign value 10 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘B’ or ‘b’ using the if conditional statement.
  • If it is true, then assign value 11 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘C’ or ‘c’ using the if conditional statement.
  • If it is true, then assign value 12 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘D’ or ‘d’ using the if conditional statement.
  • If it is true, then assign value 13 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘E’ or ‘e’ using the if conditional statement.
  • If it is true, then assign value 14 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘F’ or ‘f’ using the if conditional statement.
  • If it is true, then assign value 15 to the above-initialized rslt_val.
  • Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it into integer using the int() function.
  • Add this with the deciml_num and store it in the same variable deciml_num.
  • Decrease the value of k by 1 and store it in the same variable k.
  • Take a variable say ‘tempry ‘and initialize its value with 1.
  • Take another variable say octl_num and initialize its value with 0.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Multiply the above-initialized tempry variable with the decimal number modulus 8 and store it in a variable ‘a’.
  • Add ‘a’ with the above-initialized octl_num and store it in the same variable octl_num.
  • Divide the decimal number by 8 and store it in the same variable deciml_num.
  • Multiply the above variable tempry with 10 and convert it into an integer using the int() function.
  • Store it in the same variable tempry.
  • Print the octl_num to get the octal value of the given hexadecimal number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the hexadecimal string as static input and store it in a variable.
hexa_num = "5B"
# Take a variable say 'rslt_val 'and initialize its value with 0.
rslt_val = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Calculate the length of the given hexadecimal number and store it in a variable k.
k = len(hexa_num)
# Decrement the value of k by 1 and store it in the same variable k.
k -= 1
print("The Octal value of the given Hexadecimal number {", hexa_num, "} is: ")
# Loop from 0 to the length of the given hexadecimal number using the for loop.
for itr in range(0, len(hexa_num)):
    # Inside the loop, check if the iterator value of the hexadecimal string is greater than or
    # equal 0 and less than or equal to 9 using the if conditional statement.
    if hexa_num[itr] >= '0' and hexa_num[itr] <= '9':
        # If it is true, then convert the iterator value of the hexadecimal string into an integer
        # using the int() function.
        # Assign its value to the above-initialized rslt_val.
        rslt_val = (int)(hexa_num[itr])
# Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the
# if conditional statement.
# If it is true, then assign value 10 to the above-initialized rslt_val.
    if hexa_num[itr] == 'A' or hexa_num[itr] == 'a':
        rslt_val = 10
 # Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the
# if conditional statement.
# If it is true, then assign value 11 to the above-initialized rslt_val.
    if hexa_num[itr] == 'B' or hexa_num[itr] == 'b':
        rslt_val = 11
 # Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the
# if conditional statement.
# If it is true, then assign value 12 to the above-initialized rslt_val.
    if hexa_num[itr] == 'C' or hexa_num[itr] == 'c':
        rslt_val = 12
 # Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the
# if conditional statement.
# If it is true, then assign value 13 to the above-initialized rslt_val.
    if hexa_num[itr] == 'D' or hexa_num[itr] == 'd':
        rslt_val = 13
 # Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the
# if conditional statement.
# If it is true, then assign value 14 to the above-initialized rslt_val.
    if hexa_num[itr] == 'E' or hexa_num[itr] == 'e':
        rslt_val = 14
# Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the
# if conditional statement.
# If it is true, then assign value 15 to the above-initialized rslt_val.
    if hexa_num[itr] == 'F' or hexa_num[itr] == 'f':
        rslt_val = 15
 # Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it
# into integer using the int() function.
# Add this with the deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k))
 # Decrease the value of k by 1 and store it in the same variable k.
    k -= 1
# Take a variable say 'tempry 'and initialize its value with 1.
tempry = 1
# Take another variable say octl_num and initialize its value with 0.
octl_num = 0
# Loop till the decimal number is not equal to zero using the while loop.
while(deciml_num != 0):
  # Multiply the above-initialized tempry variable with the decimal number modulus 8 and
    # store it in a variable 'a'.
    a = (deciml_num % 8)*tempry
  # Add 'a' with the above-initialized octl_num and store it in the same variable octl_num.
    octl_num = octl_num+a
 # Divide the decimal number by 8 and store it in the same variable deciml_num.
    deciml_num = deciml_num//8
# Multiply the above variable tempry with 10 and convert it into an integer using the int()function.
# Store it in the same variable tempry.
    tempry = int(tempry*10)
# Print the octl_num to get the octal value of the given hexadecimal number.
print(octl_num)

Output:

The Octal value of the given Hexadecimal number { 5B } is: 
133

Method #2: Using For loop (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the hexadecimal string as user input using the input() function and store it in a variable.
  • Take a variable say ‘rslt_val ‘and initialize its value with 0.
  • Take another variable say deciml_num and initialize its value with 0.
  • Calculate the length of the given hexadecimal number and store it in a variable k.
  • Decrement the value of k by 1 and store it in the same variable k.
  • Loop from 0 to the length of the given hexadecimal number using the for loop.
  • Inside the loop, check if the iterator value of the hexadecimal string is greater than or equal to 0 and less than or equal to 9 using the if conditional statement.
  • If it is true, then convert the iterator value of the hexadecimal string into an integer using the int() function.
  • Assign its value to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘A’ or ‘a’ using the if conditional statement.
  • If it is true, then assign value 10 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘B’ or ‘b’ using the if conditional statement.
  • If it is true, then assign value 11 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘C’ or ‘c’ using the if conditional statement.
  • If it is true, then assign value 12 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘D’ or ‘d’ using the if conditional statement.
  • If it is true, then assign value 13 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘E’ or ‘e’ using the if conditional statement.
  • If it is true, then assign value 14 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘F’ or ‘f’ using the if conditional statement.
  • If it is true, then assign value 15 to the above-initialized rslt_val.
  • Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it into integer using the int() function.
  • Add this with the deciml_num and store it in the same variable deciml_num.
  • Decrease the value of k by 1 and store it in the same variable k.
  • Take a variable say ‘tempry ‘and initialize its value with 1.
  • Take another variable say octl_num and initialize its value with 0.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Multiply the above-initialized tempry variable with the decimal number modulus 8 and store it in a variable ‘a’.
  • Add ‘a’ with the above-initialized octl_num and store it in the same variable octl_num.
  • Divide the decimal number by 8 and store it in the same variable deciml_num.
  • Multiply the above variable tempry with 10 and convert it into an integer using the int() function.
  • Store it in the same variable tempry.
  • Print the octl_num to get the octal value of the given hexadecimal number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the hexadecimal string as user input using the input() function 
# and store it in a variable.
hexa_num = input("Enter some Random String Number = ")
# Take a variable say 'rslt_val 'and initialize its value with 0.
rslt_val = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Calculate the length of the given hexadecimal number and store it in a variable k.
k = len(hexa_num)
# Decrement the value of k by 1 and store it in the same variable k.
k -= 1
print("The Octal value of the given Hexadecimal number {", hexa_num, "} is: ")
# Loop from 0 to the length of the given hexadecimal number using the for loop.
for itr in range(0, len(hexa_num)):
    # Inside the loop, check if the iterator value of the hexadecimal string is greater than or
    # equal 0 and less than or equal to 9 using the if conditional statement.
    if hexa_num[itr] >= '0' and hexa_num[itr] <= '9':
        # If it is true, then convert the iterator value of the hexadecimal string into an integer
        # using the int() function.
        # Assign its value to the above-initialized rslt_val.
        rslt_val = (int)(hexa_num[itr])
# Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the
# if conditional statement.
# If it is true, then assign value 10 to the above-initialized rslt_val.
    if hexa_num[itr] == 'A' or hexa_num[itr] == 'a':
        rslt_val = 10
 # Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the
# if conditional statement.
# If it is true, then assign value 11 to the above-initialized rslt_val.
    if hexa_num[itr] == 'B' or hexa_num[itr] == 'b':
        rslt_val = 11
 # Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the
# if conditional statement.
# If it is true, then assign value 12 to the above-initialized rslt_val.
    if hexa_num[itr] == 'C' or hexa_num[itr] == 'c':
        rslt_val = 12
 # Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the
# if conditional statement.
# If it is true, then assign value 13 to the above-initialized rslt_val.
    if hexa_num[itr] == 'D' or hexa_num[itr] == 'd':
        rslt_val = 13
 # Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the
# if conditional statement.
# If it is true, then assign value 14 to the above-initialized rslt_val.
    if hexa_num[itr] == 'E' or hexa_num[itr] == 'e':
        rslt_val = 14
# Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the
# if conditional statement.
# If it is true, then assign value 15 to the above-initialized rslt_val.
    if hexa_num[itr] == 'F' or hexa_num[itr] == 'f':
        rslt_val = 15
 # Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it
# into integer using the int() function.
# Add this with the deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k))
 # Decrease the value of k by 1 and store it in the same variable k.
    k -= 1
# Take a variable say 'tempry 'and initialize its value with 1.
tempry = 1
# Take another variable say octl_num and initialize its value with 0.
octl_num = 0
# Loop till the decimal number is not equal to zero using the while loop.
while(deciml_num != 0):
  # Multiply the above-initialized tempry variable with the decimal number modulus 8 and
    # store it in a variable 'a'.
    a = (deciml_num % 8)*tempry
  # Add 'a' with the above-initialized octl_num and store it in the same variable octl_num.
    octl_num = octl_num+a
 # Divide the decimal number by 8 and store it in the same variable deciml_num.
    deciml_num = deciml_num//8
# Multiply the above variable tempry with 10 and convert it into an integer using the int()function.
# Store it in the same variable tempry.
    tempry = int(tempry*10)
# Print the octl_num to get the octal value of the given hexadecimal number.
print(octl_num)

Output:

Enter some Random String Number = 3DA
The Octal value of the given Hexadecimal number { 3DA } is: 
1732

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

Python Program to Convert Hexadecimal To Octal Read More »

Program to Convert Hexadecimal to Decimal

Python Program to Convert Hexadecimal to Decimal

In the previous article, we have discussed Python Program to Convert Octal to Binary
Given a hexadecimal number and the task is to get the respective decimal number.

Examples:

Example1:

Input:

Given Hexadecimal String = "2C"

Output:

The Decimal value of the given Hexadecimal number { 2C } is: 
44

Example2:

Input:

Given Hexadecimal String = "6AB"

Output:

The Decimal value of the given Hexadecimal number { 6AB } is: 
1707

Program to Convert Hexadecimal to Decimal in Python

Below are the ways to convert the given hexadecimal number into decimal :

Method #1: Using While Loop (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the hexadecimal string as static input and store it in a variable.
  • Take a variable say ‘rslt_val ‘and initialize its value with 0.
  • Take another variable say deciml_num and initialize its value with 0.
  • Calculate the length of the given hexadecimal number and store it in a variable k.
  • Decrement the value of k by 1 and store it in the same variable k.
  • Loop from 0 to the length of the given hexadecimal number using the for loop.
  • Inside the loop, check if the iterator value of the hexadecimal string is greater than or equal to 0 and less than or equal to 9 using the if conditional statement.
  • If it is true, then convert the iterator value of the hexadecimal string into an integer using the int() function.
  • Assign its value to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘A’ or ‘a’ using the if conditional statement.
  • If it is true, then assign value 10 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘B’ or ‘b’ using the if conditional statement.
  • If it is true, then assign value 11 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘C’ or ‘c’ using the if conditional statement.
  • If it is true, then assign value 12 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘D’ or ‘d’ using the if conditional statement.
  • If it is true, then assign value 13 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘E’ or ‘e’ using the if conditional statement.
  • If it is true, then assign value 14 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘F’ or ‘f’ using the if conditional statement.
  • If it is true, then assign value 15 to the above-initialized rslt_val.
  • Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it into integer using the int() function.
  • Add this with the deciml_num and store it in the same variable deciml_num.
  • Decrease the value of k by 1 and store it in the same variable k.
  • Print the deciml_num to get the decimal value of the given hexadecimal number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the hexadecimal string as static input and store it in a variable.
hexa_num = "2C"
# Take a variable say 'rslt_val 'and initialize its value with 0.
rslt_val = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Calculate the length of the given hexadecimal number and store it in a variable k.
k = len(hexa_num)
# Decrement the value of k by 1 and store it in the same variable k.
k -= 1
print(
    "The Decimal value of the given Hexadecimal number {", hexa_num, "} is: ")
# Loop from 0 to the length of the given hexadecimal number using the for loop.
for itr in range(0, len(hexa_num)):
  # Inside the loop, check if the iterator value of the hexadecimal string is greater than 0
    # and less than 9 using the if conditional statement.
    if hexa_num[itr] >= '0' and hexa_num[itr] <= '9':
     # If it is true, then convert the iterator value of the hexadecimal string into an integer
        # using the int() function.
        # Assign its value to the above-initialized rslt_val.
        rslt_val = (int)(hexa_num[itr])
# Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the
# if conditional statement.
# If it is true, then assign value 10 to the above-initialized rslt_val.
    if hexa_num[itr] == 'A' or hexa_num[itr] == 'a':
        rslt_val = 10
# Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the
# if conditional statement.
# If it is true, then assign value 11 to the above-initialized rslt_val.
    if hexa_num[itr] == 'B' or hexa_num[itr] == 'b':
        rslt_val = 11
 # Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the
# if conditional statement.
# If it is true, then assign value 12 to the above-initialized rslt_val.
    if hexa_num[itr] == 'C' or hexa_num[itr] == 'c':
        rslt_val = 12
# Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the
# if conditional statement.
# If it is true, then assign value 13 to the above-initialized rslt_val.
    if hexa_num[itr] == 'D' or hexa_num[itr] == 'd':
        rslt_val = 13
# Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the
# if conditional statement.
# If it is true, then assign value 14 to the above-initialized rslt_val.
    if hexa_num[itr] == 'E' or hexa_num[itr] == 'e':
        rslt_val = 14
# Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the
# if conditional statement.
# If it is true, then assign value 15 to the above-initialized rslt_val.
    if hexa_num[itr] == 'F' or hexa_num[itr] == 'f':
        rslt_val = 15
# Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it
# into integer using the int() function.
# Add this with the deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k))
# Decrease the value of k by 1 and store it in the same variable k.
    k -= 1
# Print the deciml_num to get the decimal value of the given hexadecimal number.
print(deciml_num)

Output:

The Decimal value of the given Hexadecimal number { 2C } is: 
44

Method #2: Using While loop (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the hexadecimal string as user input using the input() function and store it in a variable.
  • Take a variable say ‘rslt_val ‘and initialize its value with 0.
  • Take another variable say deciml_num and initialize its value with 0.
  • Calculate the length of the given hexadecimal number and store it in a variable k.
  • Decrement the value of k by 1 and store it in the same variable k.
  • Loop from 0 to the length of the given hexadecimal number using the for loop.
  • Inside the loop, check if the iterator value of the hexadecimal string is greater than 0 and less than 9 using the if conditional statement.
  • If it is true, then convert the iterator value of the hexadecimal string into an integer using the int() function.
  • Assign its value to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘A’ or ‘a’ using the if conditional statement.
  • If it is true, then assign value 10 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘B’ or ‘b’ using the if conditional statement.
  • If it is true, then assign value 11 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘C’ or ‘c’ using the if conditional statement.
  • If it is true, then assign value 12 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘D’ or ‘d’ using the if conditional statement.
  • If it is true, then assign value 13 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘E’ or ‘e’ using the if conditional statement.
  • If it is true, then assign value 14 to the above-initialized rslt_val.
  • Check if the iterator value of the hexadecimal string is equal to ‘F’ or ‘f’ using the if conditional statement.
  • If it is true, then assign value 15 to the above-initialized rslt_val.
  • Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it into integer using the int() function.
  • Add this with the deciml_num and store it in the same variable deciml_num.
  • Decrease the value of k by 1 and store it in the same variable k.
  • Print the deciml_num to get the decimal value of the given hexadecimal number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the hexadecimal string as user input using the input() function and 
# store it in a variable.
hexa_num = input("Enter some random String Number = ")
# Take a variable say 'rslt_val 'and initialize its value with 0.
rslt_val = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Calculate the length of the given hexadecimal number and store it in a variable k.
k = len(hexa_num)
# Decrement the value of k by 1 and store it in the same variable k.
k -= 1
print(
    "The Decimal value of the given Hexadecimal number {", hexa_num, "} is: ")
# Loop from 0 to the length of the given hexadecimal number using the for loop.
for itr in range(0, len(hexa_num)):
  # Inside the loop, check if the iterator value of the hexadecimal string is greater than 0
    # and less than 9 using the if conditional statement.
    if hexa_num[itr] >= '0' and hexa_num[itr] <= '9':
     # If it is true, then convert the iterator value of the hexadecimal string into an integer
        # using the int() function.
        # Assign its value to the above-initialized rslt_val.
        rslt_val = (int)(hexa_num[itr])
# Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the
# if conditional statement.
# If it is true, then assign value 10 to the above-initialized rslt_val.
    if hexa_num[itr] == 'A' or hexa_num[itr] == 'a':
        rslt_val = 10
# Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the
# if conditional statement.
# If it is true, then assign value 11 to the above-initialized rslt_val.
    if hexa_num[itr] == 'B' or hexa_num[itr] == 'b':
        rslt_val = 11
 # Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the
# if conditional statement.
# If it is true, then assign value 12 to the above-initialized rslt_val.
    if hexa_num[itr] == 'C' or hexa_num[itr] == 'c':
        rslt_val = 12
# Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the
# if conditional statement.
# If it is true, then assign value 13 to the above-initialized rslt_val.
    if hexa_num[itr] == 'D' or hexa_num[itr] == 'd':
        rslt_val = 13
# Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the
# if conditional statement.
# If it is true, then assign value 14 to the above-initialized rslt_val.
    if hexa_num[itr] == 'E' or hexa_num[itr] == 'e':
        rslt_val = 14
# Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the
# if conditional statement.
# If it is true, then assign value 15 to the above-initialized rslt_val.
    if hexa_num[itr] == 'F' or hexa_num[itr] == 'f':
        rslt_val = 15
# Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it
# into integer using the int() function.
# Add this with the deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k))
# Decrease the value of k by 1 and store it in the same variable k.
    k -= 1
# Print the deciml_num to get the decimal value of the given hexadecimal number.
print(deciml_num)

Output:

Enter some random String Number = 6AB
The Decimal value of the given Hexadecimal number { 6AB } is: 
1707

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

Python Program to Convert Hexadecimal to Decimal Read More »

Program to Convert Octal to Binary

Python Program to Convert Octal to Binary

In the previous article, we have discussed Python Program to Find the Position of an Element in a Matrix
Given an octal number and the task is to get the respective binary number using the while loop.

Examples:

Example1:

Input:

Given Octal Number = 10

Output:

The Binary value of the given Octal number { 10 } is: 
1000

Example2:

Input:

Given Octal Number = 35

Output:

The Binary value of the given Octal number { 35 } is: 
11101

Program to Convert Octal to Binary in Python

Below are the ways to convert the given octal number into binary :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the octal number as static input and store it in a variable.
  • Take a variable say ‘a ‘and initialize its value with 0.
  • Take another variable say deciml_num and initialize its value with 0.
  • Again take another variable say ‘binry_num’ and initialize its value with 0.
  • Loop till the given octal number is not equal to zero using the while loop.
  • Inside the loop, calculate the given octal number modulus 10 (to get the last digit).
  • Store it in a variable ‘b‘.
  • Calculate the value of 8 raised to the power ‘a‘ using the pow() function and multiply it with the above-obtained value ‘b‘.
  • Store it in a variable ‘c‘.
  • Add ‘c‘ to the above-initialized deciml_num and store it in the same variable deciml_num.
  • Increment the value a by 1 and store it in the same variable ‘a’.
  • Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
  • Come out of the while loop and initialize the value of ‘a’ with 1.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Multiply  ‘a‘ with the deciml_num modulus 2 and store it in a variable ‘d‘.
  • Add the value of ‘d’ with the above-initialized binry_num and store it in the same variable binry_num.
  • Divide the deciml_num by 2 and store it in the same variable deciml_num.
  • Multiply ‘a’ with 10 and store it in the same variable ‘a’.
  • Print the binry_num to get the binary value of the given octal number.
  • The Exit of the Program.

Below is the implementation:

# Give the octal number as static input and store it in a variable.
gvn_octl_num = 10
# Take a variable say 'a 'and initialize its value with 0.
a = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Again take another variable say 'binry_num' and initialize its value with 0.
binry_num = 0
# Loop till the given octal number is not equal to zero using the while loop.
print("The Binary value of the given Octal number {", gvn_octl_num, "} is: ")
while (gvn_octl_num != 0):
  # Inside the loop, calculate the given octal number modulus 10 (to get the last digit).
    # Store it in a variable 'b'.
    b = (gvn_octl_num % 10)
  # Calculate the value of 8 raised to the power 'a' using the pow() function and multiply
# it with the above-obtained value 'b'.
# Store it in a variable 'c'.
    c = pow(8, a)*b
  # Add 'c' to the above-initialized deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num + c
  # Increment the value a by 1 and store it in the same variable 'a'.
    a += 1
   # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
    gvn_octl_num = gvn_octl_num // 10
 # Come out of the while loop and initialize the value of 'a' with 1.
a = 1
# Loop till the decimal number is not equal to zero using the while loop.
while (deciml_num != 0):
  # Multiply  'a' with the deciml_num modulus 2 and store it in a variable 'd'.
    d = (deciml_num % 2) * a
  # Add the value of 'd' with the above-initialized binry_num and store it in the same
# variable binry_num.
    binry_num = binry_num + d
# Divide the deciml_num by 2 and store it in the same variable deciml_num.
    deciml_num = deciml_num // 2
# Multiply 'a' with 10 and store it in the same variable 'a'.
    a = a * 10
 # Print the binry_num to get the binary value of the given octal number.
print(binry_num)

Output:

The Binary value of the given Octal number { 10 } is: 
1000

Method #2: Using While loop (User Input)

Approach:

  • Give the octal number as user input using the int(input()) function and store it in a variable.
  • Take a variable say ‘a ‘and initialize its value with 0.
  • Take another variable say deciml_num and initialize its value with 0.
  • Again take another variable say ‘binry_num’ and initialize its value with 0.
  • Loop till the given octal number is not equal to zero using the while loop.
  • Inside the loop, calculate the given octal number modulus 10 (to get the last digit).
  • Store it in a variable ‘b‘.
  • Calculate the value of 8 raised to the power ‘a‘ using the pow() function and multiply it with the above-obtained value ‘b‘.
  • Store it in a variable ‘c‘.
  • Add ‘c‘ to the above-initialized deciml_num and store it in the same variable deciml_num.
  • Increment the value a by 1 and store it in the same variable ‘a’.
  • Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
  • Come out of the while loop and initialize the value of ‘a’ with 1.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Multiply  ‘a‘ with the deciml_num modulus 2 and store it in a variable ‘d‘.
  • Add the value of ‘d’ with the above-initialized binry_num and store it in the same variable binry_num.
  • Divide the deciml_num by 2 and store it in the same variable deciml_num.
  • Multiply ‘a’ with 10 and store it in the same variable ‘a’.
  • Print the binry_num to get the binary value of the given octal number.
  • The Exit of the Program.

Below is the implementation:

# Give the octal number as user input using the int(input()) function 
# and store it in a variable.
gvn_octl_num = int(input("Enter some Random Number = "))
# Take a variable say 'a 'and initialize its value with 0.
a = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Again take another variable say 'binry_num' and initialize its value with 0.
binry_num = 0
# Loop till the given octal number is not equal to zero using the while loop.
print("The Binary value of the given Octal number {", gvn_octl_num, "} is: ")
while (gvn_octl_num != 0):
  # Inside the loop, calculate the given octal number modulus 10 (to get the last digit).
    # Store it in a variable 'b'.
    b = (gvn_octl_num % 10)
  # Calculate the value of 8 raised to the power 'a' using the pow() function and multiply
# it with the above-obtained value 'b'.
# Store it in a variable 'c'.
    c = pow(8, a)*b
  # Add 'c' to the above-initialized deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num + c
  # Increment the value a by 1 and store it in the same variable 'a'.
    a += 1
   # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
    gvn_octl_num = gvn_octl_num // 10
 # Come out of the while loop and initialize the value of 'a' with 1.
a = 1
# Loop till the decimal number is not equal to zero using the while loop.
while (deciml_num != 0):
  # Multiply  'a' with the deciml_num modulus 2 and store it in a variable 'd'.
    d = (deciml_num % 2) * a
  # Add the value of 'd' with the above-initialized binry_num and store it in the same
# variable binry_num.
    binry_num = binry_num + d
# Divide the deciml_num by 2 and store it in the same variable deciml_num.
    deciml_num = deciml_num // 2
# Multiply 'a' with 10 and store it in the same variable 'a'.
    a = a * 10
 # Print the binry_num to get the binary value of the given octal number.
print(binry_num)

Output:

Enter some Random Number = 35
The Binary value of the given Octal number { 35 } is: 
11101

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

Python Program to Convert Octal to Binary Read More »