C Program to Convert Decimal to Octal Number

June 5, 2021

This program converts the input decimal number to an octal number.

Example: Program to convert decimal to octal

In this program, we have created a user defined function decimalToOctal() for decimal to octal conversion. The program takes the decimal number(entered by user) as input and converts it into an octal number using the function. To understand this program, you should be familiar with the following C programming concepts:

  1. C Programming User defined functions
  2. while loop in C programming
#include <stdio.h>
#include <math.h>
/* This function converts the decimal number "decimalnum"
 * to the equivalent octal number
 */
int decimalToOctal(int decimalnum)
{
    int octalnum = 0, temp = 1;

    while (decimalnum != 0)
    {
    	octalnum = octalnum + (decimalnum % 8) * temp;
    	decimalnum = decimalnum / 8;
        temp = temp * 10;
    }

    return octalnum;
}
int main()
{
    int decimalnum;

    printf("Enter a Decimal Number: ");
    scanf("%d", &decimalnum);

    printf("Equivalent Octal Number: %d", decimalToOctal(decimalnum));

    return 0;
}

Output:

Enter a Decimal Number: 436
Equivalent Octal Number: 664

admin