fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3. int num1[10],num2[10],ANS[10];
  4. void dectohex(int a)
  5. {
  6. switch(a)
  7. {
  8. case 10:printf("A");
  9. break;
  10. case 11:printf("B");
  11. break;
  12. case 12:printf("C");
  13. break;
  14. case 13:printf("D");
  15. break;
  16. case 14:printf("E");
  17. break;
  18. case 15:printf("F");
  19. break;
  20. }
  21. }
  22. void Add()
  23. {
  24. int i,j,carry,ans,flag;
  25. int len1,len2;
  26. char str1[10],str2[10];
  27. printf("Enter first number");
  28. scanf("%s",&str1);
  29. printf("Enter second number");
  30. scanf("%s",&str2);
  31. len1=strlen(str1);
  32. len2=strlen(str2);
  33. for(i=len1-1,j=9;i>=0;i--,j--)
  34. {
  35. if(str1[i]>='0'&&str1[i]<='9')
  36. {
  37. num1[j]=str1[i]-'0';
  38. }
  39. else if(str1[i]>='A'&&str1[i]<='F')
  40. {
  41. num1[j]=str1[i]-55;
  42. }
  43. }
  44. for(i=len2-1,j=9;i>=0;i--,j--)
  45. {
  46. if(str2[i]>='0'&&str2[i]<='9')
  47. {
  48. num2[j]=str2[i]-'0';
  49. }
  50. else if(str2[i]>='A'&&str2[i]<='F')
  51. {
  52. num2[j]=str2[i]-55;
  53. }
  54. }
  55. carry=0;
  56. for(i=9;i>=0;i--)
  57. {
  58. ans=num1[i]+num1[i]+carry;
  59. if(ans>=16)
  60. {
  61. carry=ans/16;
  62. }
  63. else
  64. {
  65. carry=0;
  66. }
  67. ANS[i]=ans%16;
  68. }
  69. flag=0;
  70. printf("Result=");
  71. for(i=0;i<10;i++)
  72. {
  73. if(flag==0&&ANS[i]>0)
  74. {
  75. if(ANS[i]>9)
  76. {
  77. dectohex(ANS[i]);
  78. }
  79. else
  80. {
  81. printf("%d",ANS[i]);
  82. flag=1;
  83. }
  84. }
  85. else if(flag==1)
  86. {
  87. if(ANS[i]>9)
  88. {
  89. dectohex(ANS[i]);
  90. }
  91. else
  92. {
  93. printf("%d",ANS[i]);
  94. }
  95. }
  96. }
  97. }
  98. void Multiply()
  99. {
  100. int i,j,carry,ans,flag, k;
  101. int len1,len2;
  102. char str1[10],str2[10];
  103. printf("Enter first number");
  104. scanf("%s",&str1);
  105. printf("Enter second number");
  106. scanf("%s",&str2);
  107. len1=strlen(str1);
  108. len2=strlen(str2);
  109. for(i=len1-1,j=9;i>=0;i--,j--)
  110. {
  111. if(str1[i]>='0'&&str1[i]<='9')
  112. {
  113. num1[j]=str1[i]-'0';
  114. }
  115. else if(str1[i]>='A'&&str1[i]<='F')
  116. {
  117. num1[j]=str1[i]-55;
  118. }
  119. }
  120. for(i=len2-1,j=9;i>=0;i--,j--)
  121. {
  122. if(str2[i]>='0'&&str2[i]<='9')
  123. {
  124. num2[j]=str2[i]-'0';
  125. }
  126. else if(str2[i]>='A'&&str2[i]<='F')
  127. {
  128. num2[j]=str2[i]-55;
  129. }
  130. }
  131. carry=0;
  132. for(i=9;i>=0;i--)
  133. {
  134. for(j=9;j>=0;j--)
  135. {
  136. ans=num1[j]*num2[i]+carry;
  137. if(ans>=16)
  138. {
  139. carry=ans/16;
  140. }
  141. else
  142. {
  143. carry=0;
  144. }
  145. k=i;
  146. ANS[k]+=ans%16;
  147. k--;
  148. }
  149. }
  150. flag=0;
  151. printf("Result=");
  152. for(i=0;i<10;i++)
  153. {
  154. if(flag==0&&ANS[i]>0)
  155. {
  156. if(ANS[i]>9)
  157. {
  158. dectohex(ANS[i]);
  159. }
  160. else
  161. {
  162. printf("%d",ANS[i]);
  163. flag=1;
  164. }
  165. }
  166. else if(flag==1)
  167. {
  168. if(ANS[i]>9)
  169. {
  170. dectohex(ANS[i]);
  171. }
  172. else
  173. {
  174. printf("%d",ANS[i]);
  175. }
  176. }
  177. }
  178. }
  179. void main()
  180. {
  181. int opt;
  182. clrscr();
  183. printf("HEXADECIMAL ARITHMETIC\n1.Addition\n2.Multiplication\n3.Exit");
  184. printf("\nEnter your option:");
  185. scanf("%d",&opt);
  186. switch(opt)
  187. {
  188. case 1:
  189. Add();
  190. break;
  191. case 2:Multiply();
  192. break;
  193. case 3:return;
  194. }
  195. getch();
  196. }
  197.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
2
AB 
AB
compilation info
prog.c:28:13: warning: format specifies type 'char *' but the argument has type 'char (*)[10]' [-Wformat]
 scanf("%s",&str1);
        ~~  ^~~~~
prog.c:30:13: warning: format specifies type 'char *' but the argument has type 'char (*)[10]' [-Wformat]
 scanf("%s",&str2);
        ~~  ^~~~~
prog.c:104:13: warning: format specifies type 'char *' but the argument has type 'char (*)[10]' [-Wformat]
 scanf("%s",&str1);
        ~~  ^~~~~
prog.c:106:13: warning: format specifies type 'char *' but the argument has type 'char (*)[10]' [-Wformat]
 scanf("%s",&str2);
        ~~  ^~~~~
prog.c:179:1: error: 'main' must return 'int'
void main()
^~~~
int
prog.c:182:2: warning: implicit declaration of function 'clrscr' is invalid in C99 [-Wimplicit-function-declaration]
 clrscr();
 ^
prog.c:195:2: warning: implicit declaration of function 'getch' is invalid in C99 [-Wimplicit-function-declaration]
 getch();
 ^
6 warnings and 1 error generated.
stdout
Standard output is empty