C Program For Octal to Decimal Conversion
Last Updated :
08 Jun, 2023
The number system is one of the ways to represent numbers. Every number system has its own base or radix. For example, Binary, Octal, Decimal, and Hexadecimal Number systems are some of the number systems and are also used in microprocessor programming. These numbers are easy to convert from one system to another system. One can convert decimal to binary, decimal to hex, decimal to octal, and vice versa.
Example:
Input: octal number = 123
Output: decimal number = 83
Using Format Specifier
Below is the C program to convert octal to a decimal using a format specifier:
C
#include <stdio.h>
int main()
{
int n;
printf ( "Enter an Octal number: \n" );
scanf ( "%o" , &n);
printf ( "\nDecimal Representation is: %d" , n);
return 0;
}
|
Output
Enter an Octal number: 123
Decimal Representation is: 83
Without using pow() function
Below is the C program to convert octal to decimal without using pow() function:
C
#include <stdio.h>
int octalToDecimal( int n)
{
int decimalvalue = 0;
int base = 1;
int temp = n;
while (temp)
{
int lastdigit = temp % 10;
temp = temp / 10;
decimalvalue += lastdigit * base;
base = base * 8;
}
return decimalvalue;
}
int main()
{
int octalnum = 123;
printf ( "decimal number is %d" ,
octalToDecimal(octalnum));
}
|
Output
decimal number is 83
Using pow() Function
Below is the C program to convert octal to decimal using the pow() function:
C
#include <math.h>
#include <stdio.h>
int Octaltodecimal( int octal)
{
int decimalnumber = 0, i = 0;
while (octal != 0)
{
decimalnumber = decimalnumber +
(octal % 10) * pow (8, i++);
octal = octal / 10;
}
return decimalnumber;
}
int main()
{
int octalnumber = 123;
printf ( "decimal number is %d" ,
Octaltodecimal(octalnumber));
return 0;
}
|
Output
decimal number is 83
Standard method(Without Using Functions)
Below is the C program to convert octal to decimals using the standard method:
C
#include <math.h>
#include <stdio.h>
int main()
{
int octalnumber = 123, decimalnumber = 0;
int i = 0;
while (octalnumber != 0)
{
decimalnumber = decimalnumber
+ (octalnumber % 10) * pow (8, i++);
octalnumber = octalnumber / 10;
}
printf ( "decimal number is: %d" , decimalnumber);
return 0;
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...