We need to write a **C program** to convert a **decimal number into an Octal number**.

**Example:**

1 2 3 4 5 | Input: 7 Output: 7 Input: 8 Output: 10 |

## Convert Decimal to Octal in C using While Loop

This is the **standard way** of converting a number from decimal number system to octal number system. In this method, we are implementing the same which is depicted in the above picture.

We first need a **decimal number as input** then we need to **divide the decimal number by 8 until the number reduces to 0** using while loop. In the meantime, we also have to **concatenate each remainder according to bottom-up approach** (i.e to the left of the concatenated remainders).

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 26 | #include <stdio.h> #include <stdlib.h> int main() { int n, octal=0, rem, i=1; //Input the decimal number printf("Enter a Decimal number \n"); scanf("%d",&n); //While loop will run until n becomes by dividing it by 8 while(n != 0) { rem = n%8; octal = (rem*i) + octal; //adding the remainder on left of octal n = n/8; i= i*10; //update i, so that on next iteration, // rem will be added to left of octal } printf("Octal: %d", octal); return 0; } |

**Output**

## Convert Decimal to Octal in C using Recursion

The concept used in this c program is the same as the above only difference is that, it is implemented using recursion. Since recursion implements stack so the remainder gets printed from the last.

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 26 | #include <stdio.h> #include <stdlib.h> void decToOct(int n){ if(n > 0){ //recall by passing quotient decToOct(n/8); //print remainder printf("%d",n%8); } } int main() { int n; //Input the decimal number printf("Enter a Decimal number \n"); scanf("%d",&n); printf("Octal: "); decToOct(n); return 0; } |

**Output**

comment below your doubts.