Decimal to octal in C

I just started teaching myself C from KN King C programming: a modern approach (2ndEdn).

I like it, but I hope to post an odd question here if the advice is appropriate, because, unfortunately, I don't have a teacher, and some bits raise more questions than they answer!

I ask a question about entering an integer and displaying it in octal. It says that there is an easy way to do this, but this happens later in the book. I came up with the following:

// Convert a number to octal

int n, n2, n3, n4, n5, n6;

printf("Enter a number between 0 and 32767: ");

scanf("%d", &n);

n6 = n % 8;
n5 = (n / 8) % 8;
n4 = ((n / 8) / 8) % 8;
n3 = (((n / 8) / 8) / 8) % 8;
n2 = ((((n / 8) / 8) / 8) / 8) % 8;

printf("%d%d%d%d%d", n2, n3, n4, n5, n6);

This works fine, but I don’t know how to do math and I wonder if there is a more efficient way to do this, or I did it the only way possible ...

If anyone else has a book, this is Q4 p .71.

Thank you for your time. Andrew

P.S , , ""!

+5
9

, printf. ?

, , , . :

  • Loop while n != 0
  • 3 n d d
  • n 3

, , ( shift , ).

+5

, , printf() %o :

scanf("%d", &n);
printf("%o", n);
+3

, , . , :

void print_oct(int n)
{
    if (n != 0) {
        print_oct(n / 8);
        printf("%d", n % 8);
    }
}

n > 0.

+3

:

for (int d = 8 * 8 * 8 * 8; d > 0; d /= 8)
    printf("%d", n / d % 8);
printf("\n");

d 8 * 8 * 8 * 8, , n2, 8 * 8 * 8, 8 * 8, 8 , , 1, n6, .

, , , . , .

8 3 . , .

+1

%o printf

printf("Enter a number between 0 and 32767: ");
scanf("%d", &n);
printf("%o", n);
0
/* Converts a positive base_10 into base_b */
int DecimalToBase(int n, int b)
{
    int rslt=0, digitPos=1;
    while (n)
    {
        rslt += (n%b)*digitPos;
        n /= b;
        digitPos *= 10;
    }
    return rslt;
}
0

, ( , ) , , , %o . :

int n, d1, d2, d3, d4, d5, o;

printf("Enter a number between 0 and 32767: ");
scanf("%d", &n);

d5 = n % 8;
n /= 8;
d4 = n % 8;
n /= 8;
d3 = n % 8;
n /= 8;
d2 = n % 8;
n /= 8;
d1 = n % 8;

o = 10000 * d1 + 1000 * d2 + 100 * d3 + 10 * d4 + d5;

printf("In octal, your number is: %.5d\n", o);

, n , () ( , , ). 32767 ( : 77777), 32768 (8 * 8 * 8 * 8 * 8 = 8 ^ 5 = (2 ^ 3) ^ 5 = 2 ^ 15) , : 100000.

o , , int 16- ( ), .

0

. :

#include <stdio.h>

#define OCTALBASE    8
#define OCTALSIZE    8

int main(int argc, char **argv) {
  int indecimal = 1337;
  char output[OCTALSIZE + 1];
  output[OCTALSIZE] = '\0';

  int outindex = OCTALSIZE;
  int outdigit = 0;
  int outvalue = indecimal;
  while (--outindex >= 0) {
    outdigit = outvalue % OCTALBASE;
    if (outvalue > 0 || outdigit > 0)
      { output[outindex] = '0' + outdigit; }
    else { output[outindex] = ' '; }
    outvalue /= OCTALBASE;
  }

  fprintf(stdout, "{ DEC: %8d, OCT: %s }\n", indecimal, output);
  fflush(stdout);

  return 0;
}

:

{ DEC:     1337, OCT:     2471 }
0

Convert decimal to octal in C

#include<stdio.h>
#include<conio.h>
void main()
{
    A:
    long int n,n1,m=1,rem,ans=0;
    clrscr();
    printf("\nEnter Your Decimal No :: ");
    scanf("%ld",&n);

    n1=n;
    while(n>0)
    {
        rem=n%8;
        ans=(rem*m)+ans;
        n=n/8;
        m=m*10;
    }

    printf("\nYour Decimal No is :: %ld",n1);
    printf("\nConvert into Octal No is :: %ld",ans);

    printf("\n\nPress 0 to Continue...");
    if(getch()=='0')
        goto A;
    printf("\n\n\n\tThank You");
    getch();
}
0
source

All Articles