Advertisement
Guest User

Computer Number Code Converter (C Language)

a guest
Oct 16th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.57 KB | None | 0 0
  1.  
  2. //Can someone help me with Case:11 its not working//
  3. Thank You.
  4.  
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<math.h>
  9. #include<conio.h>
  10. #include<windows.h>
  11. #define p printf
  12. #define s scanf
  13. #include<stdlib.h>
  14.  
  15. COORD coord={0,0};
  16.  
  17.  void gotoxy(int x,int y)
  18.  {
  19.    coord.X=x;
  20.  coord.Y=y;
  21.  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
  22.  }
  23. main()
  24.  
  25. {
  26.     char username[20];
  27.     char password[20];
  28.     int i=2;
  29.     int d;
  30.     char ch;
  31.     do
  32.     {
  33.         system("COLOR F0");
  34.         gotoxy(10,2);p("welcome please enter username and password");
  35.     gotoxy(10,3);printf("Enter User: ");
  36.     scanf("%s",&username);
  37.     gotoxy(10,4);printf("Enter Password: ");
  38.     s("%s",password);
  39.     if(strcmp(username, "elpost")==0)
  40.     {
  41.         if(strcmp(password, "epf")==0)
  42.  
  43.         {
  44. int choice;
  45.  
  46.     system("cls");
  47.         gotoxy(20,2);printf("Welcome To Code Converter\n");
  48. ulit:
  49.  
  50. gotoxy(20,2);p("*********CHOICES*********");
  51. gotoxy(20,3);p("1. Binary To Octal\n");
  52. gotoxy(20,4);p("2. Binary to Decimal\n");
  53. gotoxy(20,5);p("3. Binary to Hexadecimal\n");
  54. gotoxy(20,6);p("4. Octal to Binary\n");
  55. gotoxy(20,7);p("5. Octal to Decimal\n");
  56. gotoxy(20,8);p("6. Octal to Hexadecimal\n");
  57. gotoxy(20,9);p("7. Decimal to Binary\n");
  58. gotoxy(20,10);p("8. Decimal to Octal\n");
  59. gotoxy(20,11);p("9. Decimal to Hexadecimal\n");
  60. gotoxy(20,12);p("10. Hexadecimal to Binary\n");
  61. gotoxy(20,13);p("11. Hexadecimal to Octal\n");
  62. gotoxy(20,14);p("12. Hexadecimal to Decimal\n");
  63. gotoxy(20,16);p("Enter your choice:");
  64. s("%d",&choice);
  65. switch(choice)
  66. {
  67. case 1:
  68. {
  69.  int octalNum[]={0,1,10,11,100,101,110,111};
  70.  long int binary,i,number,temp,octal=0,j=1;
  71.  gotoxy(20,17);printf("\n\t\tEnter Binary number up to 10 digits: ");
  72.  scanf("%ld",&binary);
  73.   {
  74.  temp=binary;
  75.  while(temp!=0)
  76.  {
  77.   number=temp%1000;
  78.   for(i=0;i<=7;i++)
  79.   {
  80.    if(octalNum[i]==number)
  81.    {
  82.     octal=octal+(i*j);
  83.    }
  84.   }
  85.   j*=10;
  86.   temp/=1000;
  87.  }
  88.  gotoxy(20,19);printf("\n\t\tOctal number of %ld is %ld\n",binary,octal);
  89.  }
  90. goto again;
  91. }
  92. case 3:
  93.    {
  94.     int binaryNum[]={0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111},j=0,i;
  95.  char hexa[20]="";
  96.  unsigned long long int binary,number,temp;
  97.  gotoxy(20,18);printf("\n\t\tEnter Binary number up to 10 digits:");
  98.  scanf("%llu",&binary);
  99.  temp=binary;
  100.  while(temp!=0)
  101.  {
  102.   number=temp%10000;
  103.   for(i=0;i<=15;i++)
  104.   {
  105.    if(binaryNum[i]==number)
  106.    {
  107.     if(i<=9)
  108.     hexa[j]=(i+'0');
  109.     else
  110.     hexa[j]=(i+55);
  111.  
  112.     j++;
  113.    }
  114.   }
  115.   temp/=10000;
  116.  }
  117.  hexa[j]='\0';
  118.  strrev(hexa);
  119.  gotoxy(20,19);printf("\n\t\tHexadecimal number of %llu is %s\n",binary,hexa);
  120. goto again;
  121.    }
  122. case 4:
  123. {
  124.  char *octalNum[]={"000","001","010","011","100","101","110","111"};
  125.  int i=0,number,j=1;
  126.  long long int octal,temp;
  127.  char binary[20][6];
  128.  gotoxy(20,18);printf("\n\t\tEnter Octal number up to 10 digits: ");
  129.  scanf("%lld",&octal);
  130.  temp=octal;
  131.  while(temp!=0)
  132.  {
  133.   number=temp%10;
  134.   if(number>=0 && number<=7)
  135.   {
  136.   strcpy(binary[i],octalNum[number]);
  137.    temp/=10;
  138.    i++;
  139.   }
  140.   else
  141.   {
  142.    gotoxy(20,19);printf("\n\t\tInvalid Octal Number\n");
  143.    goto again;
  144.   }
  145.  }
  146.  gotoxy(20,19);printf("\n\t\tBinary number of %lld is ",octal);
  147.  for(j=i-1;j>=0;j--)
  148.  printf("%s",binary[j]);
  149.  goto again;
  150. }
  151. case 5:
  152. {
  153. int dec=0,temp,i,inc=0,octal;
  154. gotoxy(20,17);printf("\n\t\tEnter octal number up to 10 digits: ");
  155. scanf("%d",&octal);
  156. temp=octal;
  157. while(temp!=0)
  158. {
  159.     i=temp%10;
  160.     dec=dec+(i*pow(8,inc));
  161.     temp=temp/10;
  162.     inc++;
  163. }
  164. gotoxy(20,19);printf("n\t\tDecimal number of %d is %d\n",octal,dec);
  165. goto again;
  166. }
  167. case 6:
  168. {
  169.  int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};
  170.  
  171.     long long octal, tempOctal, binary, place;
  172.     char hex[65] = "";
  173.     int rem;
  174.  
  175.     place = 1;
  176.     binary = 0;
  177.  
  178.  
  179.     gotoxy(20,18);printf("\n\t\tEnter any octal number up to 10 digits: ");
  180.     scanf("%lld", &octal);
  181.     tempOctal = octal;
  182.  
  183.  
  184.     while(tempOctal > 0)
  185.     {
  186.         rem = tempOctal % 10;
  187.         binary = (OCTALVALUES[rem] * place) + binary;
  188.         tempOctal /= 10;
  189.  
  190.         place *= 1000;
  191.     }
  192.  
  193.     while(binary > 0)
  194.     {
  195.         rem = binary % 10000;
  196.         switch(rem)
  197.         {
  198.             case 0:
  199.                 strcat(hex, "0");
  200.                 break;
  201.             case 1:
  202.                 strcat(hex, "1");
  203.                 break;
  204.             case 10:
  205.                 strcat(hex, "2");
  206.                 break;
  207.             case 11:
  208.                 strcat(hex, "3");
  209.                 break;
  210.             case 100:
  211.                 strcat(hex, "4");
  212.                 break;
  213.             case 101:
  214.                 strcat(hex, "5");
  215.                 break;
  216.             case 110:
  217.                 strcat(hex, "6");
  218.                 break;
  219.             case 111:
  220.                 strcat(hex, "7");
  221.                 break;
  222.             case 1000:
  223.                 strcat(hex, "8");
  224.                 break;
  225.             case 1001:
  226.                 strcat(hex, "9");
  227.                 break;
  228.             case 1010:
  229.                 strcat(hex, "A");
  230.                 break;
  231.             case 1011:
  232.                 strcat(hex, "B");
  233.                 break;
  234.             case 1100:
  235.                 strcat(hex, "C");
  236.                 break;
  237.             case 1101:
  238.                 strcat(hex, "D");
  239.                 break;
  240.             case 1110:
  241.                 strcat(hex, "E");
  242.                 break;
  243.             case 1111:
  244.                 strcat(hex, "F");
  245.             break;
  246.         }
  247.  
  248.         binary /= 10000;
  249.     }
  250.     gotoxy(20,19);printf("\n\t\tOctal number: %lld\n", octal);
  251.     gotoxy(20,20);printf("\n\t\tHexadecimal number: %s\n", hex);
  252.     goto again;
  253. }
  254. case 7:
  255. {
  256. int dec,temp,i,j=1,binary=0;
  257. gotoxy(20,18);printf("\n\t\tEnter decimal number up to 10 digits: ");
  258. scanf("%d",&dec);
  259. temp=dec;
  260. while(temp!=0)
  261. {
  262. i=temp%2;
  263. binary=binary+(i*j);
  264. temp=temp/2;
  265. j=j*10;
  266. }
  267. gotoxy(20,19);printf("\n\t\tBinary number of %d is %d\n",dec,binary);
  268. goto again;
  269. }
  270. case 8:
  271. {
  272. int dec,temp,i,j=1,octal=0;
  273. gotoxy(20,18);printf("\n\t\tEnter decimal number up to 10 digits: ");
  274. scanf("%d",&dec);
  275. temp=dec;
  276. while(temp!=0)
  277. {
  278. i=temp%8;
  279. octal=octal+(i*j);
  280. temp=temp/8;
  281. j=j*10;
  282. }
  283. gotoxy(20,19);printf("\n\t\tOctal number of %d is %d \n",dec,octal);
  284. goto again;
  285. }
  286. case 9:
  287. {    long int decimalNumber,quotient;
  288.     int i=1,j,temp;
  289.     char hexadecimalNumber[100];
  290.     gotoxy(20,18);printf("\n\t\tEnter any decimal number up to 10 digits: ");
  291.     scanf("%ld",&decimalNumber);
  292.     quotient = decimalNumber;
  293.     while(quotient!=0) {
  294.         temp = quotient % 16;
  295.         if( temp < 10)
  296.                    temp =temp + 48; else
  297.                  temp = temp + 55;
  298.         hexadecimalNumber[i++]= temp;
  299.         quotient = quotient / 16;
  300.     }
  301.     gotoxy(20,19);printf("\n\t\tEquivalent hexadecimal value is :");
  302.     for (j = i -1 ;j> 0;j--)
  303. printf("%c",hexadecimalNumber[j]);
  304.           goto again;
  305. }
  306. case 10:
  307. {
  308. long int i=0;
  309.     char binnum[100], hexdec[100];
  310.     gotoxy(20,18);printf("\n\t\tEnter any hexadecimal number up to 10 digits: ");
  311.     scanf("%s",hexdec);
  312.     gotoxy(20,19);printf("\n\t\tEquivalent Binary value is : ");
  313.     while(hexdec[i])
  314.     {
  315.         switch(hexdec[i])
  316.         {
  317.             case '0' : printf("0000");
  318.                 break;
  319.             case '1' : printf("0001");
  320.                 break;
  321.             case '2' : printf("0010");
  322.                 break;
  323.             case '3' : printf("0011");
  324.                 break;
  325.             case '4' : printf("0100");
  326.                 break;
  327.             case '5' : printf("0101");
  328.                 break;
  329.             case '6' : printf("0110");
  330.                 break;
  331.             case '7' : printf("0111");
  332.                 break;
  333.             case '8' : printf("1000");
  334.                 break;
  335.             case '9' : printf("1001");
  336.                 break;
  337.             case 'A' : printf("1010");
  338.                 break;
  339.             case 'B' : printf("1011");
  340.                 break;
  341.             case 'C' : printf("1100");
  342.                 break;
  343.             case 'D' : printf("1101");
  344.                 break;
  345.             case 'E' : printf("1110");
  346.                 break;
  347.             case 'F' : printf("1111");
  348.                 break;
  349.             case 'a' : printf("1010");
  350.                 break;
  351.             case 'b' : printf("1011");
  352.                 break;
  353.             case 'c' : printf("1100");
  354.                 break;
  355.             case 'd' : printf("1101");
  356.                 break;
  357.             case 'e' : printf("1110");
  358.                 break;
  359.             case 'f' : printf("1111");
  360.                 break;
  361.             default :gotoxy(20,19); printf("\n\t\tInvalid hexadecimal digit %s",hexdec[i]);
  362.         }
  363.         i++;
  364.     goto again;
  365. }
  366. }
  367. case 11:
  368. {
  369. char hexi[20];
  370. int hex_to_oct(char hexi[])
  371. {
  372.     gotoxy(20,18);printf("\n\t\tEnter Hexadecimal Number up to 10 digits: ");
  373.     scanf("%s",hexi);
  374. }
  375.  
  376. {
  377.     int i,len, dec=0, oct=0;
  378.     {
  379.     for(len=0; hexi[len]!='\0'; len++);
  380.     for(i=0; hexi[i]!='\0'; i++,len--)
  381.     {
  382.         if(hexi[i]>='0' && hexi[i]<='9')
  383.         {
  384.             dec= dec + (hexi[i]-'0')*pow(16,len-1);
  385.         }
  386.         if(hexi[i]>='A' && hexi[i]<='F')
  387.         {
  388.             dec = dec + (hexi[i]-55)*pow(16,len-1);
  389.         }
  390.         if(hexi[i]>='a' && hexi[i]<='f')
  391.         {
  392.             dec = dec + (hexi[i]-87)*pow(16,len-1);
  393.         }
  394.     }
  395.     }
  396.     i=1;
  397.     while(dec!=0)
  398.     {
  399.         oct = oct + (dec%8)*i;
  400.         dec = dec/8;
  401.         i = i*10;
  402.     }
  403.     gotoxy(20,19);printf("\n\t\tEquivalent Octal Value is %d",hex_to_oct(hexi));
  404. }
  405. }
  406. case 12:
  407. {
  408.     char hexad['9'];
  409.     char *hexstr;
  410.     int length = 0;
  411.     const int base = 16;
  412.     unsigned long  decnum = 0;
  413.     int i;
  414.        gotoxy(20,18); printf("\n\t\tEnter Hexadecimal Number up to 10 digits: ");
  415.     scanf("%s", hexad);
  416.     for(hexstr = hexad; *hexstr != '\0'; hexstr++)
  417.     {
  418.     length++;
  419.     }
  420.  
  421.     hexstr = hexad;
  422.     for(i = 0; *hexstr != '\0' && i < length; i++, hexstr++)
  423.     {
  424.  
  425.     if(*hexstr >= 48 && *hexstr <= 57)
  426.     {
  427.         decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);
  428.     }
  429.     else if((*hexstr >= 65 && *hexstr <= 70))
  430.     {
  431.         decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);
  432.     }
  433.     else if(*hexstr >= 97 && *hexstr <= 102)
  434.     {
  435.         decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);
  436.     }
  437.     else
  438.     {
  439.       gotoxy(20,19);  printf("\n\t\tInvalid Hexadecimal Number \n");
  440.  
  441.     }
  442.     }
  443.  gotoxy(20,19);printf("\n\t\tEquivalent Decimal Number is %u  \n", decnum);
  444.  goto again;
  445. }
  446. default:
  447. {
  448.     gotoxy(20,18);p("invalid input");
  449.     goto again;
  450. }
  451. }
  452. char ans;
  453. again:
  454.  
  455.     p("\n\t\tWould you like to convert again? \n");
  456.         p("\n\t\tEnter y if Yes, Enter n if No: ");
  457.     s("\n\t\t%s",&ans);
  458.     switch(ans)
  459.     {
  460.     case 'y':case 'Y':
  461.         {
  462.             system("cls");
  463.  
  464.         goto ulit;
  465.         }
  466.     case 'n':case 'N':
  467.         {
  468.             system("cls");
  469.             p("\n\t\tTHANK YOU!\n");
  470.             p("\n\t\tPlease type any key to exit\n");
  471.             break;
  472.         }
  473.     default:
  474.         {
  475.             p("\n\t\tPlease enter correct choice\n");
  476.             goto again;
  477.         }
  478.     }
  479.  
  480.  
  481.  
  482.  break;
  483.         }
  484.     else
  485.  
  486.         {
  487.             system("cls");
  488.             printf("\n\t\tPassword Incorrect\n");
  489.         }
  490. }
  491.     else
  492.  
  493.     {
  494.         system("cls");
  495.         printf("\n\t\tUsername Incorrect\n");
  496.     }
  497.     i--;
  498. }while(i>=0);
  499. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement