C program to convert decimal to octal

Before diving into the C program to convert a decimal number to octal, let's briefly discuss the numerical base systems and their representation.

Numerical Base Systems

In mathematics and computer science, we frequently use different numerical base systems to represent numbers. The most common base systems are:

  1. Decimal (Base 10): This is the base system we use in our day-to-day lives. It uses ten symbols (0-9) to represent numbers. For example, 123 in decimal means 110^2 + 210^1 + 3*10^0 = 100 + 20 + 3 = 123.

  2. Binary (Base 2): In the binary system, there are only two symbols, 0 and 1. Computers often use binary because electronic circuits can easily represent these two states.

  3. Octal (Base 8): Octal uses eight symbols, 0-7. It's used in some computer programming contexts, especially in older systems. Each octal digit corresponds to three binary digits, making it convenient for some low-level programming.

  4. Hexadecimal (Base 16): Hexadecimal uses sixteen symbols, 0-9 and A-F. It is widely used in computer science to represent binary data more compactly. Each hexadecimal digit corresponds to four binary digits.

Converting Decimal to Octal

To convert a decimal number to octal, we need to repeatedly divide the decimal number by 8 and record the remainders at each step. These remainders, when read in reverse order, will give us the octal representation of the decimal number.

Now, let's look at the C program that accomplishes this conversion. The program takes a decimal number as input, performs the conversion, and displays the octal equivalent.

Here's a C program that converts a decimal number to its octal representation. I'll explain each part of the code and provide an example with its output.

#include <stdio.h>

int main() {
    int decimalNumber, remainder, quotient;
    int octalNumber[100], i = 1;

    printf("Enter a decimal number: ");
    scanf("%d", &decimalNumber);

    quotient = decimalNumber;

    while (quotient != 0) {
        octalNumber[i++] = quotient % 8;
        quotient = quotient / 8;
    }

    printf("Octal equivalent: ");
    for (int j = i - 1; j > 0; j--) {
        printf("%d", octalNumber[j]);
    }

    return 0;
}
 

Let's break down the code step by step:

  1. We include the standard input/output library (stdio.h) to use functions like printf and scanf.

  2. In the main function, we declare the following variables:

    • decimalNumber: This is the decimal number entered by the user.
    • remainder and quotient: These are temporary variables for calculations.
    • octalNumber[100]: An array to store the octal digits.
    • i: A counter for the octalNumber array.
  3. We prompt the user to enter a decimal number and store it in the decimalNumber variable.

  4. We initialize quotient with the value of decimalNumber. This is the number we will be converting to octal.

  5. We use a while loop to perform the conversion. Inside the loop:

    • remainder stores the remainder of the quotient when divided by 8, which is the next octal digit.
    • We store the remainder in the octalNumber array and increment i.
    • We update the quotient by dividing it by 8 to proceed to the next digit.
  6. After the loop, we print the octal equivalent by iterating through the octalNumber array in reverse order and printing each digit.

Now, let's run an example:

Enter a decimal number: 123
Octal equivalent: 173

In this example, we input the decimal number 123, and the program converts it to its octal equivalent, which is 173.

 

Prasun Barua

Prasun Barua is an Engineer (Electrical & Electronic) and Member of the European Energy Centre (EEC). His first published book Green Planet is all about green technologies and science. His other published books are Solar PV System Design and Technology, Electricity from Renewable Energy, Tech Know Solar PV System, C Coding Practice, AI and Robotics Overview, Robotics and Artificial Intelligence, Know How Solar PV System, Know The Product, Solar PV Technology Overview, Home Appliances Overview, Tech Know Solar PV System, C Programming Practice, etc. These books are available at Google Books, Google Play, Amazon and other platforms.

*

Post a Comment (0)
Previous Post Next Post