Advertisement
BladeMechanics

Conversion Program

Sep 28th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #define MAX 25
  4. void binary(int number)
  5. {
  6.     int binary[MAX], i, count=-1;
  7.     i = number;
  8.     do
  9.     {
  10.         i /= 2;
  11.         count++;
  12.     } while (i);
  13.     i = count;
  14.     for (count; count >= 0; count--)
  15.     {
  16.         binary[count] = number % 2;
  17.         number /= 2;
  18.     }
  19.     for (count = 0; count <= i; count++) printf("%d", binary[count]);
  20. }
  21.  
  22. void octal(int number)
  23. {
  24.     int octal[MAX], i, count = -1;
  25.     i = number;
  26.     do
  27.     {
  28.         i /= 8;
  29.         count++;
  30.     } while (i);
  31.     i = count;
  32.     for (count; count >= 0; count--)
  33.     {
  34.         octal[count] = number % 8;
  35.         number /= 8;
  36.     }
  37.     for (count = 0; count <= i; count++) printf("%d", octal[count]);
  38. }
  39. void hex(int number)
  40. {
  41.     int hexadec[MAX], i, count = -1;
  42.     i = number;
  43.     do
  44.     {
  45.         i /= 16;
  46.         count++;
  47.     } while (i);
  48.     i = count;
  49.     for (count; count >= 0; count--)
  50.     {
  51.         hexadec[count] = number % 16;
  52.         number /= 16;
  53.     }
  54.     for (count = 0; count <= i; count++) printf("%c",hexadec[count]<10?hexadec[count]+48:hexadec[count]+55);
  55. }
  56.  
  57. void main()
  58. {
  59. int num;
  60. printf("Please enter a number: ");
  61. scanf_s("%d", &num);
  62.  
  63. printf("\nThe number %d in binary is ", num);
  64. binary(num);
  65. printf("\nThe number %d in octal is ", num);
  66. octal(num);
  67. printf("\nThe number %d in hex is ",num);
  68. hex(num);
  69. _getch();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement