Decimal To Octal Conversion | C Program

Decimal to Octal Conversion in C

Here, in this section we will discuss decimal to octal conversion in C.

The C program to convert decimal to octal number accepts a decimal number from the user. This number is further converted to its equivalent octal number after following a series of steps. The following section gives an algorithm for this conversion. It is then followed by a C program.

Ex:- If a Decimal number is convert of octal number we use this method that is show in fig(1).

(143)10 (Decimal number)=(217)8 (Octal equivalent)

Decimal to octal

Algorithm:-

  • Take a decimal number as input from the user and store it in variable say dec_num.
  • Create an array as octalno[] to store the octal representation of the given octal number.
  • Create a variable say quotient and initialize it with dec_num value.
  • Run a loop that will terminate when quotient becomes 0.
  • Inside loop put the remainder that achieved by dividing the quottient with 8 in the array octalno.
  • After coming out from the loop print the octalno array in reverse order to get the required converted octal number.

Example: Input – 717

  • 717/8, Remainder = 5, Quotient = 89 
  • 89/8, Remainder = 1, Quotient = 11
  • 11/8, Remainder = 3, Quotient = 1    
  • 1/8, Remainder = 1, Quotient = 0

Hence, 717 in decimal = 1315 in octal.

Competitive Coding Techniques

C Program for Decimal to Octal Conversion

//Program to convert Decimal number into octal number
#include<stdio.h> 

int main()
 {
//Variable initialization
     long dec_num, rem, quotient;     
     int i, j, octalno[100];
//Taking input from user
     printf("Enter a number for conversion: ");
//Storing the value in dec_num variable     
     scanf("%ld",&dec_num);
     quotient = dec_num;
     i=1;
//Storing the octal value in octalno[] array
     while (quotient!=0)
       {
        octalno[i++]=quotient%8;
        quotient=quotient/8;
       }
//Printing the octalno [] in reverse order
     printf("\nThe Octal of %ld is:\n\n",dec_num);
     for (j=i-1;j>0;j--)
     //display it
printf ("%d", octalno[j]); return 0; }

Output

Enter a number for conversion: 79
The octal of 79 is 117

Optimized Code

Algorithm :

  • Instead of creating array.
  • Declare a variable say octal_num and initialize it with 0 and one variable say base and initialize it with 1.
  • Inside the loop set octal_num = octal_num + rem*base
  • And increment base as base*10 in each iteration.
#include<stdio.h>

int main()
{
int n,decimal,rem,sum=0,base=1;
printf(“enter a decimal number\n”);
scanf(“%d”,&decimal);
n=decimal;
while(decimal>0)
{
rem=decimal%8;
sum=sum+rem*base;
decimal=decimal/8;
base=base*10;
}
printf(“the octal format is %d “,sum);
}

 

2 comments on “Decimal To Octal Conversion | C Program”


  • J

    #include

    int main()
    {
    int n,decimal,rem,sum=0,base=1;
    printf(“enter a decimal number\n”);
    scanf(“%d”,&decimal);
    n=decimal;
    while(decimal>0)
    {
    rem=decimal%8;
    sum=sum+rem*base;
    decimal=decimal/8;
    base=base*10;
    }
    printf(“the octal format is %d “,sum);
    }
    simplest form for above progam