PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Conversion

Program to convert octal number into decimal number

Input - Output

Input
Enter a Octal number : 17

Output
Decimal equivalent of given number : 15

Input
Enter a Octal number : 24

Output
Decimal equivalent of given number : 20

Algorithm

Step 1: START

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

Step 3: Multiply each digit of num by the power of 8 and store each multiplication.

i.e Power of 8 starts from 0 to n-1 where n is the total number of digits in the num

Step 4: END

CODE


/* Octal to Decimal Conversion */

#include <stdio.h>
#include <math.h>
int main()
{
      int oct,decimal=0,rem,i,j;
      printf(" Enter the octal number : ");
      scanf("%d",&oct);
      for(i=0;oct>0;i++)
    {
        rem=oct%10;
        decimal=decimal+rem*pow(8,i);
        oct/=10;
    }
      printf(" decimal of given number is = %d",decimal);
    return 0;
}


/* Octal to Decimal Conversion */

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
      int oct,decimal=0,rem,i,j;
      cout<<" Enter the octal number : ";
      cin>>oct;
      for(i=0;oct>0;i++)
    {
        rem=oct%10;
        decimal=decimal+rem*pow(8,i);
        oct/=10;
    }
      cout<<" decimal of given number is = "<<decimal;
    return 0;
}

OutPut

Enter a Octal number : 17

Decimal equivalent of given number : 15