fork(1) download
  1. #include <stdio.h>
  2.  
  3. void decimal_to_octal(int num) {
  4. int result[15] = {};
  5. int i = 0, j = 0;
  6.  
  7. if(num >= 8) {
  8. printf("Decimal: %d -> Octal: ", num);
  9.  
  10. while(num >= 8) {
  11. result[i++] = num % 8;
  12. num = num / 8;
  13. }
  14. result[i] = num;
  15.  
  16. for(j = i; j >= 0; j--) {
  17. printf("%d", result[j]);
  18. }
  19. printf("\n");
  20. } else {
  21. printf("Decimal: %d -> Octal: %d\n", num, num);
  22. }
  23. }
  24.  
  25. int main(void) {
  26.  
  27. decimal_to_octal(7);
  28. decimal_to_octal(8);
  29. decimal_to_octal(10);
  30. decimal_to_octal(568);
  31. decimal_to_octal(3578);
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
Decimal: 7 -> Octal: 7
Decimal: 8 -> Octal: 10
Decimal: 10 -> Octal: 12
Decimal: 568 -> Octal: 1070
Decimal: 3578 -> Octal: 6772