c
  1. c-program-to-convert-octal-number-to-decimal-and-vice-versa

Program to Convert Octal Number to Decimal and Vice-Versa (C Program)

Heading

This program is used to convert a given octal number to decimal number or vice-versa.

Example

Input:

Enter 1 to convert octal to decimal
Enter 2 to convert decimal to octal
1
Enter an octal number: 345

Output:

The decimal conversion of 345 is: 181

Explanation

The program takes an input choice from the user to convert octal to decimal or decimal to octal. If the choice is 1, the program takes an octal number as input from the user and converts it into its equivalent decimal number. If the choice is 2, the program takes a decimal number as input from the user and converts it into its equivalent octal number.

Conversion from Octal to Decimal

To convert an octal number to decimal, we need to multiply each digit of the octal number with its corresponding power of 8 and add them together.

Example:

         3        4        5
  3 * 8^2 + 4 * 8^1 + 5 * 8^0 = 181

Conversion from Decimal to Octal

To convert a decimal number to octal, we need to keep dividing the decimal number by 8 until the quotient becomes 0. At each division, we need to divide the quotient by 8 and note down the remainder in reverse order.

Example:

  181 / 8 = 22 Remainder 5
  22 / 8 = 2 Remainder 6
  2 / 8 = 0 Remainder 2
  Octal number = 256

Use

This program can be useful for converting octal numbers into their equivalent decimal numbers or vice-versa. It is commonly used in various fields such as computer programming, electronics, and digital systems.

Summary

The program is used to convert an octal number to its equivalent decimal number or a decimal number to its equivalent octal number. The program makes use of mathematical formulas such as multiplying digits with their corresponding power of 8 to convert octal to decimal and dividing the decimal number by 8 to convert decimal to octal. This program can be useful in various fields such as computer programming, electronics, and digital systems.

Published on: