2020-05-02 19:52:26 +03:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
// Converts octal number to decimal
|
2020-05-29 23:23:24 +03:00
|
|
|
int convertValue(int num, int i) { return num * pow(8, i); }
|
2020-05-02 19:52:26 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
long long toDecimal(int octal_value)
|
|
|
|
{
|
2020-05-02 19:52:26 +03:00
|
|
|
int decimal_value = 0, i = 0;
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
while (octal_value)
|
|
|
|
{
|
2020-05-02 19:52:26 +03:00
|
|
|
// Extracts right-most digit and then multiplies by 8^i
|
|
|
|
decimal_value += convertValue(octal_value % 10, i++);
|
|
|
|
|
|
|
|
// Shift right in base 10
|
|
|
|
octal_value /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return decimal_value;
|
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int main()
|
|
|
|
{
|
2020-05-02 19:52:26 +03:00
|
|
|
printf("Enter octal value: ");
|
|
|
|
|
|
|
|
int octal_value;
|
|
|
|
|
|
|
|
scanf("%d", &octal_value);
|
|
|
|
|
|
|
|
long long result = toDecimal(octal_value);
|
|
|
|
|
|
|
|
printf("%d in decimal is %lld\n", octal_value, result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|