C Program to Convert Decimal to Octal

In this article, we will learn how to create a program in C that converts any given decimal number (given by the user at run-time) to its equivalent octal number. At last, we have also created a user-defined function program that does the same job.

Before going through the program, if you are not aware of

then refer to the step-by-step process for decimal to octal conversion. Now let's move on to the program.

Decimal to octal in C

To convert a decimal number to an octal number in C programming, you have to ask the user to enter the decimal number, convert it to an octal number, and then display the equivalent value in octal as an output, as shown here in the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, octnum[50], i=0;
    printf("Enter any Decimal number: ");
    scanf("%d", &decnum);
    while(decnum != 0)
    {
        octnum[i] = decnum%8;
        i++;
        decnum = decnum/8;
    }
    printf("\nEquivalent Octal Value = ");
    for(i=(i-1); i>=0; i--)
        printf("%d", octnum[i]);
    getch();
    return 0;
}

As the above program was written in the Code::Blocks IDE, here is the sample run after a successful build and run. This is the first snapshot of the sample run:

c program convert decimal to octal

Now supply any decimal number, say 159, and press the ENTER key to see its equivalent octal value as shown in the second snapshot of the sample run given here:

decimal to octal in c

Program Explained

Decimal to Octal in C without a Modulus Operator

Now, let's make the same program that converts any given decimal number into its equivalent octal value without using any modulus operators, as shown in the code below.

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, octnum[100], i=0, temp, chck, rem;
    printf("Enter any Decimal number: ");
    scanf("%d", &decnum);
    while(decnum!=0)
    {
        temp = decnum/8;
        chck = temp*8;
        rem = decnum - chck;
        octnum[i] = rem;
        i++;
        decnum = temp;
    }
    printf("\nEquivalent Octal Value = ");
    for(i=i-1; i>=0; i--)
        printf("%d", octnum[i]);
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c decimal to octal without modulous

Program Explained

Therefore, we have successfully replaced the modulus operator and created the program for the same purpose but without using any modulus operators as shown in the above program.

Decimal to Octal in C using a User-Defined Function

Now let's create a function-driven program for the same purpose. The question is, "Write a program in C that converts a decimal number to an octal number using a user-defined function." The answer to this question is:

#include<stdio.h>
#include<conio.h>
void DecToOct(int dec);
int octnum[50];
static int i;
int main()
{
    int decnum;
    printf("Enter any Decimal number: ");
    scanf("%d", &decnum);
    DecToOct(decnum);
    printf("\nEquivalent Octal Value = ");
    for(i=(i-1); i>=0; i--)
        printf("%d", octnum[i]);
    getch();
}
void DecToOct(int dec)
{
    while(dec != 0)
    {
        octnum[i] = dec%8;
        i++;
        dec = dec/8;
    }
}

Here is the final snapshot of the sample run of the above program:

c decimal to octal using function

Here we have declared the variable i as a static variable. because a static variable remembers its previous value. And we have not initialized the value of i with 0, because a static variable holds 0 as its initial value. That is, if you do not assign any value to any static variable, 0 is assigned to it automatically. We have created the array octnum[] and the variable i outside the main() function so that both the array and variable stay known throughout the program, that is, inside both the functions, namely, main() and DecToOct().

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!