# Convert Decimal To Octal Number C Program

## C Program To Convert Decimal To Octal Number

Learn How To Convert Decimal To Octal Number in C Programming Language. This C code for Decimal Number to Octal Conversion makes use of **pow()** function, **While** loop and** For** loop.

#### Decimal Number

A Decimal Number has a base 10 and includes digits the following digits: **0 1 2 3 4 5 6 7 8 9**

#### Octal Number

An Octal Number has a base 8 and includes the following digits: **0 1 2 3 4 5 6 7**

#### Method 1: C Program To Convert Decimal To Octal Number using While Loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<stdio.h> #include<math.h> int main() { int decimal_number, remainder, octal_number = 0; int count = 0; printf("Enter a Decimal Number:\t"); scanf("%d", &decimal_number); while(decimal_number != 0) { remainder = decimal_number % 8; octal_number = octal_number + remainder * pow(10, count); decimal_number = decimal_number / 8; count++; } printf("\nOctal Equivalent:\t%d\n", octal_number); return 0; } |

#### Method 2: C Program For Decimal Number To Octal Conversion using For Loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> #include<math.h> int main() { int decimal_number, remainder, octal_number = 0; int count; printf("Enter a Decimal Number:\t"); scanf("%d", &decimal_number); for(count = 0; decimal_number > 0; count++) { remainder = decimal_number % 8; octal_number = octal_number + remainder * pow(10, count); decimal_number = decimal_number / 8; } printf("\nOctal Equivalent:\t%d\n", octal_number); return 0; } |

#### Method 3: C Program To Convert Decimal Number To Octal Value using Functions

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<stdio.h> #include<math.h> int decimal_to_octal(int x) { int octal_number, remainder, count = 0; while(x != 0) { remainder = x % 8; octal_number = octal_number + remainder * pow(10, count); x = x / 8; count++; } return octal_number; } int main() { int decimal_number, result; printf("Enter a Decimal Number:\t"); scanf("%d", &decimal_number); result = decimal_to_octal(decimal_number); printf("\nOctal Equivalent of the Decimal Number:\t%d\n", result); return 0; } |

#### Output

If you have any compilation error or doubts in this C Program For Decimal Value to Octal Number Conversion, let us know about it in the Comment Section below.

Thanks. This is one of the easiest Number System Conversion Programs.

I think there is a problem with math.h header file. I am getting a compilation error when I convert decimal number to octal as per the above c program.