C program to Convert Decimal to Octal

«
»

C Program to convert Decimal number into Octal number. Decimal number is a number with base 10 whereas octal number is a number with base 8. Valid decimal numbers are (0-9) whereas valid octal numbers are (0-7). In decimal to octal conversion, we divide the decimal number by 8 and write the remainder in the reverse order to get the equivalent octal number.

Example 1:

Given Decimal Number: 85

Method followed: Divide the number by 8 until its zero and note remainder in each case.

Divide 85 by 8, number is 10, remainder is 5.
Divide 10 by 8, number is 1, remainder is 2.
Divide 1 by 8, number is 0, remainder is 1.

So the octal number is 125 (remainders in reverse order).

Example 2:

advertisement
advertisement

Given Decimal Number: 195

Divide 195 by 8, number is 24, remainder is 3.
Divide 24 by 8, number is 3, remainder is 0.
Divide 3 by 8, number is 0, remainder is 3.

So the octal number is 303.

Problem Description

Write a C program to convert decimal numbers to octal numbers.

Problem Solution

The procedure for converting decimal to octal in C is as follows.

1. Take a decimal number as input.
2. Divide the input number by 8 and obtain its remainder and quotient. Store the remainder in the array.
3. Repeat the step 2 with the quotient obtained. Do this until the quotient becomes zero.
4. Print the array in the reverse order to get the output.

Program/Source Code

Here is source code of the C program to Convert Decimal to Octal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to Convert Decimal to Octal
 */
#include <stdio.h>
 
int main()
{
    long decimalnum, remainder, quotient,octalnum=0;
    int octalNumber[100], i = 1, j;
 
    printf("Enter the decimal number: ");
    scanf("%ld", &decimalnum);
    quotient = decimalnum;
 
    //Storing remainders until number is equal to zero
    while (quotient != 0)
    {
        octalNumber[i++] = quotient % 8;
        quotient = quotient / 8;
    }
 
    //Converting stored remainder values in corresponding octal number
    for (j = i - 1; j > 0; j--)
        octalnum = octalnum*10 + octalNumber[j];
    printf("Equivalent octal value of decimal no %d is: %d  ", decimalnum,octalnum);
    return 0;
}
Program Explanation

1. Take a decimal number as input and store it in the variable decimalnum.
2. Copy the variable decimalnum to the variable quotient.
3. Divide the number by 8 and store the remainder in an array until the number is equal to 0.
4. Store the reverse of the array in number format in variable ‘octalnum’.
5. Print the octal number.

advertisement

Expected Input and Output:

Enter the decimal number: 195

Dividing 195 by 8, number is 32, octalNumber[0]=195 % 8 = 3.
Dividing 24 by 8, number is 3, octalNumber[1]=32 % 8 = 0.
Dividing 3 by 8, number is 0, octalNumber[2]=3 % 8 = 3.

Now iterate over the stored array in reverse order.
Initially octalnum=0 and for each iteration

octalnum= octalnum * 10 + octalNumber[i]

advertisement

Octalnum = (0 * 10) + octalNumber[2] = (0 * 10) + 3 = 3
Octalnum = (3 * 10) + octalNumber[1] = (3 * 10) + 0 = 30
Octalnum = (30 * 10) + octalNumber[0] = (30 * 10) + 3 = 303

Corresponding octal number is 303.

Time Complexity: O(log n)
In this program, we are dividing the number by 8 in every step, so time complexity is O(log n) with base 8.

Space Complexity: O(n)
Space is required to store the remainders, so space complexity is O(n).

Runtime Test Cases
Output:
Enter the decimal number: 195
Equivalent octal value of decimal no 195 is: 303

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.