Here, in this section, we will discuss the C program for Octal to Decimal conversion in C. Octal to Decimal in C happens with the principle that a decimal number can be attained by multiplying every digit of octal number with a power of 2 and adding each multiplication outcome. The power of the integer starts from 0 and counts to n-1 where n is assumed as the overall digits of integers in octal numbers.
// C Program to convert octal to decimal
#include<stdio.h>
#include<math.h>
// function to convert octal to decimal
int convert(long long num)
{
int i = 0, decimal = 0;
// will only work for bases 2 - 10
int base = 8;
//converting octal to decimal
while (num!=0)
{
int digit = num % 10;
decimal += digit * pow(base, i);
num /= 10;
i++;
}
return decimal;
}
//main program
int main()
{
// long used rather than int to store large values
long long octal;
printf("Enter Octal Number: ");
scanf("%lld", &octal);
printf("Decimal: %lld", convert(octal));
return 0;
}
Login/Signup to comment