Horlic Vince
Horlic Vince

Reputation: 21

Converting Octal numbers To Decimal in C

#include <stdio.h>
#include <math.h>

void main() {
    long int on, temp;
    int dn = 0, e = 0, digit;
    printf("Enter octal number : ");
    scanf("%ld", &on);
    temp = on;
    while (on != 0) {
        digit = on % 10;
        dn += digit * pow(8, e);
        e++;
        on /= 10;
    }
    printf("octal number = %ld \n", temp);
    printf("Decimal number= %d  \n", dn);

    return 1;
}

Hi, good day. I have this code to convert Octal numbers to decimal, but my question / problem is how can you make a code that will convert octal decimal numbers. Example:

0.758 → ___10

0.000018 → ___10

in converting, you must multiply each digit my the square of 8 (8,64,542...)

I need help in this.

Upvotes: 1

Views: 2672

Answers (4)

chux
chux

Reputation: 154262

If one needs to cope with fractional input like a string of "123.456" representing an octal number and having the decimal value of 83.589843750, then read the integer (whole) number portion and fractional portion as integers separately: each integer from a string of octal digits.

Good time for code re-use, so create a helper function. Code could use unsigned on; scanf("%o",&on); or code like the below.

The fraction part is a little tricky. Code below "divides" each fractional digit by 8 with a multiply of 1000/8 and the divide by 1000 (by printing groups of 3 digits to the right of .).

#include <stdio.h>
#include <limits.h>
#include <stdint.h>

static int ReadOctal(uintmax_t *octal, unsigned *count) {
  int ch;
  *octal = 0;
  *count = 0;
  while ((ch=fgetc(stdin)) >= '0' && ch <= '7') {
    (*count)++;
    if (*octal > UINTMAX_MAX/8) puts("Overflow");
    *octal = *octal*8 + (unsigned)(ch - '0');
  }
  return ch;
}

void octal_decimal(void) {
  uintmax_t ipart;
  uintmax_t fpart = 0;
  unsigned count;
  int ch = ReadOctal(&ipart, &count);
  if (ch == '.') {
    ReadOctal(&fpart, &count);
  }
  printf("%ju", ipart);
  if (ch == '.') {
    for (unsigned i=0; i<count; i++) {
      if (fpart > UINTMAX_MAX/(1000/8)) puts("Overflow");
      fpart *= 1000/8;
    }
    printf(".%0*ju", count*3, fpart);
  }
  puts("");
}

123.456 // input
83.589843750 // output

Upvotes: 1

ashiquzzaman33
ashiquzzaman33

Reputation: 5741

You can read data as Octal format.

#include<stdio.h>
#include<math.h>
int main()
{
    unsigned on;
    printf("Enter octal number : ");
    scanf("%o",&on);
    printf("octal number = %o \n",on);
    printf("Decimal number= %d  \n",on);
    return 0;
}

Literally how you read data or show data it's up to you, internal representation are same for all hex, octal and decimal. For further reading see

Upvotes: 2

r3mainer
r3mainer

Reputation: 24587

Here's a program that will input a fractional octal number and convert it to a floating-point value. An octal point and at least one following digit are required, so don't expect this to work on inputs like "100" or "100."

#include <stdio.h>
int main() {
  unsigned int integer_part, fraction_part, len1, len2;
  double result;
  scanf("%o.%n%o%n", &integer_part, &len1, &fraction_part, &len2);
  result = fraction_part * 1.0 / (1<<(len2-len1)*3);
  result += integer_part;
  printf("%.6f\n",result);
  return 0;
}

Examples:

$ ./oct <<<"0.75"
0.953125
$ ./oct <<<"100.1"
64.125000

If you want to accept a broader range of inputs (including integers), then you would be better off reading a whole line of text and parsing it piecemeal.

Upvotes: 3

user5363114
user5363114

Reputation:

Don't do it like this.

Read the data from the user into a char* buffer.

Then use strtoul(); specifying a base of 8.

Upvotes: 6

Related Questions