A C program to covert decimal to binary, octal & Hexadecimal – IGNOU MCA Assignment 2013

By | September 17, 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013

 

A C program that takes a decimal number and converts it into binary, octal and hexadecimal equivalents. Your program should have functions for each type of conversion. These functions should implement algorithms to perform these conversions. Predefined functions OR %x and %o codes are not allowed. The output of the programme should be stored in a text file named output.txt.

 

#include<stdio.h>
FILE *FP=NULL;
void main()
{
     int CH,NUM;
     void octal(int);
     void binary(int);
     void hexa(int);
     clrscr();
     printf("ENTER DECIMAL NUMBER TO BE CONVERTED : \n");
     scanf("%d",&NUM);
     printf("\nSELECT CONVERSION");
     printf("\n 1. DECIMAL TO BINARY\n");
     printf("\n 2. DECIMAL TO OCTAL\n");
     printf("\n 3. DECIMAL TO HEXADECIMAL\n");
     printf("\nENTER CHOICE HERE :");
     scanf("%d",&CH);
     switch(CH)
     {
       case 1 : binary(NUM);
        printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
        break;
       case 2 : octal(NUM);
        printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
        break;
       case 3 : hexa(NUM);
        printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
        break;
       default :   printf("\nYOU HAVE ENTERED WRONG CHOICE !!!");
}

  getch();
}
void hexa(int Y)
{
     char HEXC[5];
     int NUM,I,LEN,HEXD[5];
     NUM=Y;
     LEN=0;
     while(Y>0)
     {
    HEXD[LEN]=Y%16;
    Y=Y/16;
    LEN++;
     };
     for(I=LEN-1;I>-1;I--)
     {
    if(HEXD[I]<10)
       HEXC[I]=HEXD[I]+48;
    else
       HEXC[I]=HEXD[I]+55;
     }
     if((FP=fopen("output.txt","a+t"))==NULL)
     {
    printf("FILE CAN'T BE OPENED OR CREATED\n");
    exit(0);
     }
     fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
     for(I=LEN-1;I>-1;I--)
     {
    fprintf(FP,"%c",HEXC[I]);
     }

}
void binary(int Y)
{
     int NUM,I,LEN,BIN[20];
     NUM=Y;
     LEN=0;
     while(Y>0)
     {
    BIN[LEN]=Y%2;
    Y=Y/2;
    LEN++;
     };
     if((FP=fopen("output.txt","a+t"))==NULL)
     {
    printf("FILE CAN'T BE OPENED OR CREATED\n");
    exit(0);
     }
     fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
     for(I=LEN-1;I>-1;I--)
     {
    fprintf(FP,"%d",BIN[I]);
     }

}
void octal(int Y)
{
     int NUM,I,LEN,OCT[5];
     NUM=Y;
     LEN=0;
     while(Y>0)
     {
    OCT[LEN]=Y%8;
    Y=Y/8;
    LEN++;
     };
     if((FP=fopen("output.txt","a+t"))==NULL)
     {
    printf("FILE CAN'T BE OPENED OR CREATED\n");
    exit(0);
     }
     fprintf(FP,"\nCONVERTED OCTAL EQUIVALENT VALUE OF %d IS \t",NUM);
     for(I=LEN-1;I>-1;I--)
     {
    fprintf(FP,"%d",OCT[I]);
     }
}

CODE : -

  1. #include<stdio.h>
  2. FILE *FP=NULL;
  3. void main()
  4. {
  5. int CH,NUM;
  6. void octal(int);
  7. void binary(int);
  8. void hexa(int);
  9. clrscr();
  10. printf("ENTER DECIMAL NUMBER TO BE CONVERTED : \n");
  11. scanf("%d",&NUM);
  12. printf("\nSELECT CONVERSION");
  13. printf("\n 1. DECIMAL TO BINARY\n");
  14. printf("\n 2. DECIMAL TO OCTAL\n");
  15. printf("\n 3. DECIMAL TO HEXADECIMAL\n");
  16. printf("\nENTER CHOICE HERE :");
  17. scanf("%d",&CH);
  18. switch(CH)
  19. {
  20. case 1 : binary(NUM);
  21. printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
  22. break;
  23. case 2 : octal(NUM);
  24. printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
  25. break;
  26. case 3 : hexa(NUM);
  27. printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
  28. break;
  29. default : printf("\nYOU HAVE ENTERED WRONG CHOICE !!!");
  30. }
  31.  
  32. getch();
  33. }
  34. void hexa(int Y)
  35. {
  36. char HEXC[5];
  37. int NUM,I,LEN,HEXD[5];
  38. NUM=Y;
  39. LEN=0;
  40. while(Y>0)
  41. {
  42. HEXD[LEN]=Y%16;
  43. Y=Y/16;
  44. LEN++;
  45. };
  46. for(I=LEN-1;I>-1;I--)
  47. {
  48. if(HEXD[I]<10)
  49. HEXC[I]=HEXD[I]+48;
  50. else
  51. HEXC[I]=HEXD[I]+55;
  52. }
  53. if((FP=fopen("output.txt","a+t"))==NULL)
  54. {
  55. printf("FILE CAN'T BE OPENED OR CREATED\n");
  56. exit(0);
  57. }
  58. fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
  59. for(I=LEN-1;I>-1;I--)
  60. {
  61. fprintf(FP,"%c",HEXC[I]);
  62. }
  63.  
  64. }
  65. void binary(int Y)
  66. {
  67. int NUM,I,LEN,BIN[20];
  68. NUM=Y;
  69. LEN=0;
  70. while(Y>0)
  71. {
  72. BIN[LEN]=Y%2;
  73. Y=Y/2;
  74. LEN++;
  75. };
  76. if((FP=fopen("output.txt","a+t"))==NULL)
  77. {
  78. printf("FILE CAN'T BE OPENED OR CREATED\n");
  79. exit(0);
  80. }
  81. fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
  82. for(I=LEN-1;I>-1;I--)
  83. {
  84. fprintf(FP,"%d",BIN[I]);
  85. }
  86.  
  87. }
  88. void octal(int Y)
  89. {
  90. int NUM,I,LEN,OCT[5];
  91. NUM=Y;
  92. LEN=0;
  93. while(Y>0)
  94. {
  95. OCT[LEN]=Y%8;
  96. Y=Y/8;
  97. LEN++;
  98. };
  99. if((FP=fopen("output.txt","a+t"))==NULL)
  100. {
  101. printf("FILE CAN'T BE OPENED OR CREATED\n");
  102. exit(0);
  103. }
  104. fprintf(FP,"\nCONVERTED OCTAL EQUIVALENT VALUE OF %d IS \t",NUM);
  105. for(I=LEN-1;I>-1;I--)
  106. {
  107. fprintf(FP,"%d",OCT[I]);
  108. }
  109. }

SCREEN SHOTS : -

MCS017_Q1

MCS017_Q1_Output

MCS017_Q1_Output_txt

Leave a Reply