PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Conversion

Program to convert a decimal number into octal number

Input - Output

Input
Enter the decimal number : 19

Output
Octal equivalent of given number : 23

Input
Enter the decimal number : 12

Output
Octal equivalent of given number : 14

Algorithm

Step 1: START

Step 2: Enter a number 'num' as an input.

Step 3: Calculate remainder i.e(num%8) and store it into an array

Step 4: Divide the num by 2 i.e (num/8)

Step 5: Repeat the step 3 and 4 until the num is greater than zero

Step 6: END

CODE


/* Decimal to Octal Conversion */

#include <stdio.h>
int main()
{
      int num,oct[10],i,j;
      printf(" Enter the decimal number : ");
      scanf("%d",&num);
      for(i=0;num>0;i++)
    {
        oct[i]=num%8;
        num/=8;
    }
      printf(" Octal of given number is = ");
      for(j=i-1;j>=0;j--)
      printf("%d",oct[j]);
    return 0;
}


/* Decimal to Octal Conversion */

#include <iostream>
using namespace std;
int main()
{
      int num,oct[10],i,j;
      cout<<" Enter the decimal number : ";
      cin>>num;
      for(i=0;num>0;i++)
    {
        oct[i]=num%8;
        num/=8;
    }
      cout<<" Octal of given number is = ";
      for(j=i-1;j>=0;j--)
      cout<<oct[j];
    return 0;
}

OutPut

Enter the decimal number : 19

Octal of given number is : 23