Advertisement
Guest User

Number System Converter

a guest
Oct 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<conio.h>
  5. #include<windows.h>
  6. #define p printf
  7. #define s scanf
  8. #include<stdlib.h>
  9.  
  10. COORD coord={0,0};
  11.  
  12.  void gotoxy(int x,int y)
  13.  {
  14.    coord.X=x;
  15.  coord.Y=y;
  16.  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
  17.  }
  18. main()
  19.  
  20. {
  21.     char username[20];
  22.     char password[20];
  23.     int i=2;
  24.     int d;
  25.     char ch;
  26.     do
  27.     {
  28.         system("COLOR F0");
  29.         gotoxy(10,2);p("welcome please enter username and password");
  30.     gotoxy(10,3);printf("Enter User: ");
  31.     scanf("%s",&username);
  32.     gotoxy(10,4);printf("Enter Password: ");
  33.     s("%s",password);
  34.     if(strcmp(username, "elpost")==0)
  35.     {
  36.         if(strcmp(password, "epf")==0)
  37.  
  38.         {
  39. int choice;
  40.  
  41.     system("cls");
  42.         gotoxy(20,2);printf("Welcome To Code Converter\n");
  43. ulit:
  44.  
  45. gotoxy(20,2);p("**********CHOICES**********");
  46. gotoxy(20,3);p("1. Binary To Octal\n");
  47. gotoxy(20,4);p("2. Binary to Decimal\n");
  48. gotoxy(20,5);p("3. Binary to Hexadecimal\n");
  49. gotoxy(20,6);p("4. Octal to Binary\n");
  50. gotoxy(20,7);p("5. Octal to Decimal\n");
  51. gotoxy(20,8);p("6. Octal to Hexadecimal\n");
  52. gotoxy(20,9);p("7. Decimal to Binary\n");
  53. gotoxy(20,10);p("8. Decimal to Octal\n");
  54. gotoxy(20,11);p("9. Decimal to Hexadecimal\n");
  55. gotoxy(20,12);p("10. Hexadecimal to Binary\n");
  56. gotoxy(20,13);p("11. Hexadecimal to Octal\n");
  57. gotoxy(20,14);p("12. Hexadecimal to Decimal\n");
  58. gotoxy(20,16);p("Enter your choice:");
  59. s("%d",&choice);
  60. switch(choice)
  61. {
  62. case 1:
  63. {
  64.  int octalNum[]={0,1,10,11,100,101,110,111};
  65.  long int binary,i,number,temp,octal=0,j=1;
  66.  gotoxy(20,17);printf("\n\t\tEnter Binary number up to 10 digits: ");
  67.  scanf("%ld",&binary);
  68.   {
  69.  temp=binary;
  70.  while(temp!=0)
  71.  {
  72.   number=temp%1000;
  73.   for(i=0;i<=7;i++)
  74.   {
  75.    if(octalNum[i]==number)
  76.    {
  77.     octal=octal+(i*j);
  78.    }
  79.   }
  80.   j*=10;
  81.   temp/=1000;
  82.  }
  83.  gotoxy(20,19);printf("\n\t\tOctal number of %ld is %ld\n",binary,octal);
  84.  }
  85. goto again;
  86. }
  87.  
  88. case 2:
  89. {
  90. int dec=0,temp,i,inc=0,binary;
  91. gotoxy(20,18);printf("\n\t\tEnter a binary number up to 10 digits: ");
  92. scanf("%d",&binary);
  93. {
  94. temp=binary;
  95. while(temp!=0)
  96. {
  97. i=temp%10;
  98. dec=dec+(i*pow(2,inc));
  99. temp=temp/10;
  100. inc++;
  101. }
  102. gotoxy(20,19);printf("\n\t\tDecimal number of %d is %d\n",binary,dec);
  103. goto again;
  104. }
  105. }
  106.  
  107. case 3:
  108.    {
  109.     int binaryNum[]={0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111},j=0,i;
  110.  char hexa[20]="";
  111.  unsigned long long int binary,number,temp;
  112.  gotoxy(20,18);printf("\n\t\tEnter Binary number up to 10 digits:");
  113.  scanf("%llu",&binary);
  114.  temp=binary;
  115.  while(temp!=0)
  116.  {
  117.   number=temp%10000;
  118.   for(i=0;i<=15;i++)
  119.   {
  120.    if(binaryNum[i]==number)
  121.    {
  122.     if(i<=9)
  123.     hexa[j]=(i+'0');
  124.     else
  125.     hexa[j]=(i+55);
  126.  
  127.     j++;
  128.    }
  129.   }
  130.   temp/=10000;
  131.  }
  132.  hexa[j]='\0';
  133.  strrev(hexa);
  134.  gotoxy(20,19);printf("\n\t\tHexadecimal number of %llu is %s\n",binary,hexa);
  135. goto again;
  136.    }
  137. case 4:
  138. {
  139.  char *octalNum[]={"000","001","010","011","100","101","110","111"};
  140.  int i=0,number,j=1;
  141.  long long int octal,temp;
  142.  char binary[20][6];
  143.  gotoxy(20,18);printf("\n\t\tEnter Octal number up to 10 digits: ");
  144.  scanf("%lld",&octal);
  145.  temp=octal;
  146.  while(temp!=0)
  147.  {
  148.   number=temp%10;
  149.   if(number>=0 && number<=7)
  150.   {
  151.   strcpy(binary[i],octalNum[number]);
  152.    temp/=10;
  153.    i++;
  154.   }
  155.   else
  156.   {
  157.    gotoxy(20,19);printf("\n\t\tInvalid Octal Number\n");
  158.    goto again;
  159.   }
  160.  }
  161.  gotoxy(20,19);printf("\n\t\tBinary number of %lld is ",octal);
  162.  for(j=i-1;j>=0;j--)
  163.  printf("%s",binary[j]);
  164.  goto again;
  165. }
  166. case 5:
  167. {
  168. int dec=0,temp,i,inc=0,octal;
  169. gotoxy(20,17);printf("\n\t\tEnter octal number up to 10 digits: ");
  170. scanf("%d",&octal);
  171. temp=octal;
  172. while(temp!=0)
  173. {
  174.     i=temp%10;
  175.     dec=dec+(i*pow(8,inc));
  176.     temp=temp/10;
  177.     inc++;
  178. }
  179. gotoxy(20,19);printf("n\t\tDecimal number of %d is %d\n",octal,dec);
  180. goto again;
  181. }
  182. case 6:
  183. {
  184.  int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};
  185.  
  186.     long long octal, tempOctal, binary, place;
  187.     char hex[65] = "";
  188.     int rem;
  189.  
  190.     place = 1;
  191.     binary = 0;
  192.  
  193.  
  194.     gotoxy(20,18);printf("\n\t\tEnter any octal number up to 10 digits: ");
  195.     scanf("%lld", &octal);
  196.     tempOctal = octal;
  197.  
  198.  
  199.     while(tempOctal > 0)
  200.     {
  201.         rem = tempOctal % 10;
  202.         binary = (OCTALVALUES[rem] * place) + binary;
  203.         tempOctal /= 10;
  204.  
  205.         place *= 1000;
  206.     }
  207.  
  208.     while(binary > 0)
  209.     {
  210.         rem = binary % 10000;
  211.         switch(rem)
  212.         {
  213.             case 0:
  214.                 strcat(hex, "0");
  215.                 break;
  216.             case 1:
  217.                 strcat(hex, "1");
  218.                 break;
  219.             case 10:
  220.                 strcat(hex, "2");
  221.                 break;
  222.             case 11:
  223.                 strcat(hex, "3");
  224.                 break;
  225.             case 100:
  226.                 strcat(hex, "4");
  227.                 break;
  228.             case 101:
  229.                 strcat(hex, "5");
  230.                 break;
  231.             case 110:
  232.                 strcat(hex, "6");
  233.                 break;
  234.             case 111:
  235.                 strcat(hex, "7");
  236.                 break;
  237.             case 1000:
  238.                 strcat(hex, "8");
  239.                 break;
  240.             case 1001:
  241.                 strcat(hex, "9");
  242.                 break;
  243.             case 1010:
  244.                 strcat(hex, "A");
  245.                 break;
  246.             case 1011:
  247.                 strcat(hex, "B");
  248.                 break;
  249.             case 1100:
  250.                 strcat(hex, "C");
  251.                 break;
  252.             case 1101:
  253.                 strcat(hex, "D");
  254.                 break;
  255.             case 1110:
  256.                 strcat(hex, "E");
  257.                 break;
  258.             case 1111:
  259.                 strcat(hex, "F");
  260.             break;
  261.         }
  262.  
  263.         binary /= 10000;
  264.     }
  265.     gotoxy(20,19);printf("\n\t\tOctal number: %lld\n", octal);
  266.     gotoxy(20,20);printf("\n\t\tHexadecimal number: %s\n", hex);
  267.     goto again;
  268. }
  269. case 7:
  270. {
  271. int dec,temp,i,j=1,binary=0;
  272. gotoxy(20,18);printf("\n\t\tEnter decimal number up to 10 digits: ");
  273. scanf("%d",&dec);
  274. temp=dec;
  275. while(temp!=0)
  276. {
  277. i=temp%2;
  278. binary=binary+(i*j);
  279. temp=temp/2;
  280. j=j*10;
  281. }
  282. gotoxy(20,19);printf("\n\t\tBinary number of %d is %d\n",dec,binary);
  283. goto again;
  284. }
  285. case 8:
  286. {
  287. int dec,temp,i,j=1,octal=0;
  288. gotoxy(20,18);printf("\n\t\tEnter decimal number up to 10 digits: ");
  289. scanf("%d",&dec);
  290. temp=dec;
  291. while(temp!=0)
  292. {
  293. i=temp%8;
  294. octal=octal+(i*j);
  295. temp=temp/8;
  296. j=j*10;
  297. }
  298. gotoxy(20,19);printf("\n\t\tOctal number of %d is %d \n",dec,octal);
  299. goto again;
  300. }
  301. case 9:
  302. {    long int decimalNumber,quotient;
  303.     int i=1,j,temp;
  304.     char hexadecimalNumber[100];
  305.     gotoxy(20,18);printf("\n\t\tEnter any decimal number up to 10 digits: ");
  306.     scanf("%ld",&decimalNumber);
  307.     quotient = decimalNumber;
  308.     while(quotient!=0) {
  309.         temp = quotient % 16;
  310.         if( temp < 10)
  311.                    temp =temp + 48; else
  312.                  temp = temp + 55;
  313.         hexadecimalNumber[i++]= temp;
  314.         quotient = quotient / 16;
  315.     }
  316.     gotoxy(20,19);printf("\n\t\tEquivalent hexadecimal value is :");
  317.     for (j = i -1 ;j> 0;j--)
  318. printf("%c",hexadecimalNumber[j]);
  319.           goto again;
  320. }
  321. case 10:
  322. {
  323. long int i=0;
  324.     char binnum[100], hexdec[100];
  325.     gotoxy(20,18);printf("\n\t\tEnter any hexadecimal number up to 10 digits: ");
  326.     scanf("%s",hexdec);
  327.     gotoxy(20,19);printf("\n\t\tEquivalent Binary value is : ");
  328.     while(hexdec[i])
  329.     {
  330.         switch(hexdec[i])
  331.         {
  332.             case '0' : printf("0000");
  333.                 break;
  334.             case '1' : printf("0001");
  335.                 break;
  336.             case '2' : printf("0010");
  337.                 break;
  338.             case '3' : printf("0011");
  339.                 break;
  340.             case '4' : printf("0100");
  341.                 break;
  342.             case '5' : printf("0101");
  343.                 break;
  344.             case '6' : printf("0110");
  345.                 break;
  346.             case '7' : printf("0111");
  347.                 break;
  348.             case '8' : printf("1000");
  349.                 break;
  350.             case '9' : printf("1001");
  351.                 break;
  352.             case 'A' : printf("1010");
  353.                 break;
  354.             case 'B' : printf("1011");
  355.                 break;
  356.             case 'C' : printf("1100");
  357.                 break;
  358.             case 'D' : printf("1101");
  359.                 break;
  360.             case 'E' : printf("1110");
  361.                 break;
  362.             case 'F' : printf("1111");
  363.                 break;
  364.             case 'a' : printf("1010");
  365.                 break;
  366.             case 'b' : printf("1011");
  367.                 break;
  368.             case 'c' : printf("1100");
  369.                 break;
  370.             case 'd' : printf("1101");
  371.                 break;
  372.             case 'e' : printf("1110");
  373.                 break;
  374.             case 'f' : printf("1111");
  375.                 break;
  376.             default :gotoxy(20,19); printf("\n\t\tInvalid hexadecimal digit %s",hexdec[i]);
  377.         }
  378.         i++;
  379.     goto again;
  380. }
  381.  
  382. case 11:
  383. {
  384. char hex[20];
  385.  
  386. int hex_to_oct(char hex[])
  387. {
  388.     int i,len, dec=0, oct=0;
  389.     gotoxy(20,18);printf("\n\t\tEnter Hexadecimal Number up to 10 digits: ");
  390.     scanf("%s",hex);
  391.     gotoxy(20,19);printf("\n\t\tEquivalent Octal Value is %d",hex_to_oct(hex));
  392.     for(len=0; hex[len]!='\0'; len++);
  393.     for(i=0; hex[i]!='\0'; i++,len--)
  394.  
  395.     {
  396.         if(hex[i]>='0' && hex[i]<='9')
  397.         {
  398.             dec= dec + (hex[i]-'0')*pow(16,len-1);
  399.         }
  400.         if(hex[i]>='A' && hex[i]<='F')
  401.         {
  402.             dec = dec + (hex[i]-55)*pow(16,len-1);
  403.         }
  404.         if(hex[i]>='a' && hex[i]<='f')
  405.         {
  406.             dec = dec + (hex[i]-87)*pow(16,len-1);
  407.         }
  408.     }
  409.     i=1;
  410.     while(dec!=0)
  411.     {
  412.         oct = oct + (dec%8)*i;
  413.         dec = dec/8;
  414.         i = i*10;
  415.     }
  416. }
  417. }
  418.  
  419. case 12:
  420. {  char hex['9'];
  421.     char *hexstr;
  422.     int length = 0;
  423.     const int base = 16;
  424.     unsigned long  decnum = 0;
  425.     int i;
  426.        gotoxy(20,18); printf("\n\t\tEnter Hexadecimal Number up to 10 digits: ");
  427.     scanf("%s", hex);
  428.     for(hexstr = hex; *hexstr != '\0'; hexstr++)
  429.     {
  430.     length++;
  431.     }
  432.  
  433.     hexstr = hex;
  434.     for(i = 0; *hexstr != '\0' && i < length; i++, hexstr++)
  435.     {
  436.  
  437.     if(*hexstr >= 48 && *hexstr <= 57)
  438.     {
  439.         decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);
  440.     }
  441.     else if((*hexstr >= 65 && *hexstr <= 70))
  442.     {
  443.         decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);
  444.     }
  445.     else if(*hexstr >= 97 && *hexstr <= 102)
  446.     {
  447.         decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);
  448.     }
  449.     else
  450.     {
  451.       gotoxy(20,19);  printf("\n\t\tInvalid Hexadecimal Number \n");
  452.  
  453.     }
  454.     }
  455.  gotoxy(20,19);printf("\n\t\tEquivalent Decimal Number is %u  \n", decnum);
  456.  goto again;
  457. }
  458.  
  459. default:
  460. {
  461.     gotoxy(20,18);p("invalid input");
  462.     goto again;
  463. }
  464.  
  465. }
  466.         }
  467. char ans;
  468. again:
  469.  
  470.     p("\n\t\tWould you like to convert again? \n");
  471.         p("\n\t\tEnter y if Yes, Enter n if No: ");
  472.     s("\n\t\t%s",&ans);
  473.     switch(ans)
  474.     {
  475.     case 'y':case 'Y':
  476.         {
  477.             system("cls");
  478.  
  479.         goto ulit;
  480.         }
  481.     case 'n':case 'N':
  482.         {
  483.             system("cls");
  484.             p("\n\t\tTHANK YOU!\n");
  485.             p("\n\t\tPlease type any key to exit\n");
  486.             break;
  487.         }
  488.     default:
  489.         {
  490.             p("\n\t\tPlease enter correct choice\n");
  491.             goto again;
  492.         }
  493.     }
  494.     break;
  495.         }
  496.     else
  497.  
  498.         {
  499.             system("cls");
  500.             printf("\n\t\tPassword Incorrect\n");
  501.         }
  502. }
  503.     else
  504.  
  505.     {
  506.         system("cls");
  507.         printf("\n\t\tUsername Incorrect\n");
  508.     }
  509.     i--;
  510. }while(i>=0);
  511. }#include<stdio.h>
  512. #include<string.h>
  513. #include<math.h>
  514. #include<conio.h>
  515. #include<windows.h>
  516. #define p printf
  517. #define s scanf
  518. #include<stdlib.h>
  519.  
  520. COORD coord={0,0};
  521.  
  522.  void gotoxy(int x,int y)
  523.  {
  524.    coord.X=x;
  525.  coord.Y=y;
  526.  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
  527.  }
  528. main()
  529.  
  530. {
  531.     char username[20];
  532.     char password[20];
  533.     int i=2;
  534.     int d;
  535.     char ch;
  536.     do
  537.     {
  538.         system("COLOR F0");
  539.         gotoxy(10,2);p("welcome please enter username and password");
  540.     gotoxy(10,3);printf("Enter User: ");
  541.     scanf("%s",&username);
  542.     gotoxy(10,4);printf("Enter Password: ");
  543.     s("%s",password);
  544.     if(strcmp(username, "elpost")==0)
  545.     {
  546.         if(strcmp(password, "epf")==0)
  547.  
  548.         {
  549. int choice;
  550.  
  551.     system("cls");
  552.         gotoxy(20,2);printf("Welcome To Code Converter\n");
  553. ulit:
  554.  
  555. gotoxy(20,2);p("**********CHOICES**********");
  556. gotoxy(20,3);p("1. Binary To Octal\n");
  557. gotoxy(20,4);p("2. Binary to Decimal\n");
  558. gotoxy(20,5);p("3. Binary to Hexadecimal\n");
  559. gotoxy(20,6);p("4. Octal to Binary\n");
  560. gotoxy(20,7);p("5. Octal to Decimal\n");
  561. gotoxy(20,8);p("6. Octal to Hexadecimal\n");
  562. gotoxy(20,9);p("7. Decimal to Binary\n");
  563. gotoxy(20,10);p("8. Decimal to Octal\n");
  564. gotoxy(20,11);p("9. Decimal to Hexadecimal\n");
  565. gotoxy(20,12);p("10. Hexadecimal to Binary\n");
  566. gotoxy(20,13);p("11. Hexadecimal to Octal\n");
  567. gotoxy(20,14);p("12. Hexadecimal to Decimal\n");
  568. gotoxy(20,16);p("Enter your choice:");
  569. s("%d",&choice);
  570. switch(choice)
  571. {
  572. case 1:
  573. {
  574.  int octalNum[]={0,1,10,11,100,101,110,111};
  575.  long int binary,i,number,temp,octal=0,j=1;
  576.  gotoxy(20,17);printf("\n\t\tEnter Binary number up to 10 digits: ");
  577.  scanf("%ld",&binary);
  578.   {
  579.  temp=binary;
  580.  while(temp!=0)
  581.  {
  582.   number=temp%1000;
  583.   for(i=0;i<=7;i++)
  584.   {
  585.    if(octalNum[i]==number)
  586.    {
  587.     octal=octal+(i*j);
  588.    }
  589.   }
  590.   j*=10;
  591.   temp/=1000;
  592.  }
  593.  gotoxy(20,19);printf("\n\t\tOctal number of %ld is %ld\n",binary,octal);
  594.  }
  595. goto again;
  596. }
  597.  
  598. case 2:
  599. {
  600. int dec=0,temp,i,inc=0,binary;
  601. gotoxy(20,18);printf("\n\t\tEnter a binary number up to 10 digits: ");
  602. scanf("%d",&binary);
  603. {
  604. temp=binary;
  605. while(temp!=0)
  606. {
  607. i=temp%10;
  608. dec=dec+(i*pow(2,inc));
  609. temp=temp/10;
  610. inc++;
  611. }
  612. gotoxy(20,19);printf("\n\t\tDecimal number of %d is %d\n",binary,dec);
  613. goto again;
  614. }
  615. }
  616.  
  617. case 3:
  618.    {
  619.     int binaryNum[]={0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111},j=0,i;
  620.  char hexa[20]="";
  621.  unsigned long long int binary,number,temp;
  622.  gotoxy(20,18);printf("\n\t\tEnter Binary number up to 10 digits:");
  623.  scanf("%llu",&binary);
  624.  temp=binary;
  625.  while(temp!=0)
  626.  {
  627.   number=temp%10000;
  628.   for(i=0;i<=15;i++)
  629.   {
  630.    if(binaryNum[i]==number)
  631.    {
  632.     if(i<=9)
  633.     hexa[j]=(i+'0');
  634.     else
  635.     hexa[j]=(i+55);
  636.  
  637.     j++;
  638.    }
  639.   }
  640.   temp/=10000;
  641.  }
  642.  hexa[j]='\0';
  643.  strrev(hexa);
  644.  gotoxy(20,19);printf("\n\t\tHexadecimal number of %llu is %s\n",binary,hexa);
  645. goto again;
  646.    }
  647. case 4:
  648. {
  649.  char *octalNum[]={"000","001","010","011","100","101","110","111"};
  650.  int i=0,number,j=1;
  651.  long long int octal,temp;
  652.  char binary[20][6];
  653.  gotoxy(20,18);printf("\n\t\tEnter Octal number up to 10 digits: ");
  654.  scanf("%lld",&octal);
  655.  temp=octal;
  656.  while(temp!=0)
  657.  {
  658.   number=temp%10;
  659.   if(number>=0 && number<=7)
  660.   {
  661.   strcpy(binary[i],octalNum[number]);
  662.    temp/=10;
  663.    i++;
  664.   }
  665.   else
  666.   {
  667.    gotoxy(20,19);printf("\n\t\tInvalid Octal Number\n");
  668.    goto again;
  669.   }
  670.  }
  671.  gotoxy(20,19);printf("\n\t\tBinary number of %lld is ",octal);
  672.  for(j=i-1;j>=0;j--)
  673.  printf("%s",binary[j]);
  674.  goto again;
  675. }
  676. case 5:
  677. {
  678. int dec=0,temp,i,inc=0,octal;
  679. gotoxy(20,17);printf("\n\t\tEnter octal number up to 10 digits: ");
  680. scanf("%d",&octal);
  681. temp=octal;
  682. while(temp!=0)
  683. {
  684.     i=temp%10;
  685.     dec=dec+(i*pow(8,inc));
  686.     temp=temp/10;
  687.     inc++;
  688. }
  689. gotoxy(20,19);printf("n\t\tDecimal number of %d is %d\n",octal,dec);
  690. goto again;
  691. }
  692. case 6:
  693. {
  694.  int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};
  695.  
  696.     long long octal, tempOctal, binary, place;
  697.     char hex[65] = "";
  698.     int rem;
  699.  
  700.     place = 1;
  701.     binary = 0;
  702.  
  703.  
  704.     gotoxy(20,18);printf("\n\t\tEnter any octal number up to 10 digits: ");
  705.     scanf("%lld", &octal);
  706.     tempOctal = octal;
  707.  
  708.  
  709.     while(tempOctal > 0)
  710.     {
  711.         rem = tempOctal % 10;
  712.         binary = (OCTALVALUES[rem] * place) + binary;
  713.         tempOctal /= 10;
  714.  
  715.         place *= 1000;
  716.     }
  717.  
  718.     while(binary > 0)
  719.     {
  720.         rem = binary % 10000;
  721.         switch(rem)
  722.         {
  723.             case 0:
  724.                 strcat(hex, "0");
  725.                 break;
  726.             case 1:
  727.                 strcat(hex, "1");
  728.                 break;
  729.             case 10:
  730.                 strcat(hex, "2");
  731.                 break;
  732.             case 11:
  733.                 strcat(hex, "3");
  734.                 break;
  735.             case 100:
  736.                 strcat(hex, "4");
  737.                 break;
  738.             case 101:
  739.                 strcat(hex, "5");
  740.                 break;
  741.             case 110:
  742.                 strcat(hex, "6");
  743.                 break;
  744.             case 111:
  745.                 strcat(hex, "7");
  746.                 break;
  747.             case 1000:
  748.                 strcat(hex, "8");
  749.                 break;
  750.             case 1001:
  751.                 strcat(hex, "9");
  752.                 break;
  753.             case 1010:
  754.                 strcat(hex, "A");
  755.                 break;
  756.             case 1011:
  757.                 strcat(hex, "B");
  758.                 break;
  759.             case 1100:
  760.                 strcat(hex, "C");
  761.                 break;
  762.             case 1101:
  763.                 strcat(hex, "D");
  764.                 break;
  765.             case 1110:
  766.                 strcat(hex, "E");
  767.                 break;
  768.             case 1111:
  769.                 strcat(hex, "F");
  770.             break;
  771.         }
  772.  
  773.         binary /= 10000;
  774.     }
  775.     gotoxy(20,19);printf("\n\t\tOctal number: %lld\n", octal);
  776.     gotoxy(20,20);printf("\n\t\tHexadecimal number: %s\n", hex);
  777.     goto again;
  778. }
  779. case 7:
  780. {
  781. int dec,temp,i,j=1,binary=0;
  782. gotoxy(20,18);printf("\n\t\tEnter decimal number up to 10 digits: ");
  783. scanf("%d",&dec);
  784. temp=dec;
  785. while(temp!=0)
  786. {
  787. i=temp%2;
  788. binary=binary+(i*j);
  789. temp=temp/2;
  790. j=j*10;
  791. }
  792. gotoxy(20,19);printf("\n\t\tBinary number of %d is %d\n",dec,binary);
  793. goto again;
  794. }
  795. case 8:
  796. {
  797. int dec,temp,i,j=1,octal=0;
  798. gotoxy(20,18);printf("\n\t\tEnter decimal number up to 10 digits: ");
  799. scanf("%d",&dec);
  800. temp=dec;
  801. while(temp!=0)
  802. {
  803. i=temp%8;
  804. octal=octal+(i*j);
  805. temp=temp/8;
  806. j=j*10;
  807. }
  808. gotoxy(20,19);printf("\n\t\tOctal number of %d is %d \n",dec,octal);
  809. goto again;
  810. }
  811. case 9:
  812. {    long int decimalNumber,quotient;
  813.     int i=1,j,temp;
  814.     char hexadecimalNumber[100];
  815.     gotoxy(20,18);printf("\n\t\tEnter any decimal number up to 10 digits: ");
  816.     scanf("%ld",&decimalNumber);
  817.     quotient = decimalNumber;
  818.     while(quotient!=0) {
  819.         temp = quotient % 16;
  820.         if( temp < 10)
  821.                    temp =temp + 48; else
  822.                  temp = temp + 55;
  823.         hexadecimalNumber[i++]= temp;
  824.         quotient = quotient / 16;
  825.     }
  826.     gotoxy(20,19);printf("\n\t\tEquivalent hexadecimal value is :");
  827.     for (j = i -1 ;j> 0;j--)
  828. printf("%c",hexadecimalNumber[j]);
  829.           goto again;
  830. }
  831. case 10:
  832. {
  833. long int i=0;
  834.     char binnum[100], hexdec[100];
  835.     gotoxy(20,18);printf("\n\t\tEnter any hexadecimal number up to 10 digits: ");
  836.     scanf("%s",hexdec);
  837.     gotoxy(20,19);printf("\n\t\tEquivalent Binary value is : ");
  838.     while(hexdec[i])
  839.     {
  840.         switch(hexdec[i])
  841.         {
  842.             case '0' : printf("0000");
  843.                 break;
  844.             case '1' : printf("0001");
  845.                 break;
  846.             case '2' : printf("0010");
  847.                 break;
  848.             case '3' : printf("0011");
  849.                 break;
  850.             case '4' : printf("0100");
  851.                 break;
  852.             case '5' : printf("0101");
  853.                 break;
  854.             case '6' : printf("0110");
  855.                 break;
  856.             case '7' : printf("0111");
  857.                 break;
  858.             case '8' : printf("1000");
  859.                 break;
  860.             case '9' : printf("1001");
  861.                 break;
  862.             case 'A' : printf("1010");
  863.                 break;
  864.             case 'B' : printf("1011");
  865.                 break;
  866.             case 'C' : printf("1100");
  867.                 break;
  868.             case 'D' : printf("1101");
  869.                 break;
  870.             case 'E' : printf("1110");
  871.                 break;
  872.             case 'F' : printf("1111");
  873.                 break;
  874.             case 'a' : printf("1010");
  875.                 break;
  876.             case 'b' : printf("1011");
  877.                 break;
  878.             case 'c' : printf("1100");
  879.                 break;
  880.             case 'd' : printf("1101");
  881.                 break;
  882.             case 'e' : printf("1110");
  883.                 break;
  884.             case 'f' : printf("1111");
  885.                 break;
  886.             default :gotoxy(20,19); printf("\n\t\tInvalid hexadecimal digit %s",hexdec[i]);
  887.         }
  888.         i++;
  889.     goto again;
  890. }
  891.  
  892. case 11:
  893. {
  894. char hex[20];
  895.  
  896. int hex_to_oct(char hex[])
  897. {
  898.     int i,len, dec=0, oct=0;
  899.     gotoxy(20,18);printf("\n\t\tEnter Hexadecimal Number up to 10 digits: ");
  900.     scanf("%s",hex);
  901.     gotoxy(20,19);printf("\n\t\tEquivalent Octal Value is %d",hex_to_oct(hex));
  902.     for(len=0; hex[len]!='\0'; len++);
  903.     for(i=0; hex[i]!='\0'; i++,len--)
  904.  
  905.     {
  906.         if(hex[i]>='0' && hex[i]<='9')
  907.         {
  908.             dec= dec + (hex[i]-'0')*pow(16,len-1);
  909.         }
  910.         if(hex[i]>='A' && hex[i]<='F')
  911.         {
  912.             dec = dec + (hex[i]-55)*pow(16,len-1);
  913.         }
  914.         if(hex[i]>='a' && hex[i]<='f')
  915.         {
  916.             dec = dec + (hex[i]-87)*pow(16,len-1);
  917.         }
  918.     }
  919.     i=1;
  920.     while(dec!=0)
  921.     {
  922.         oct = oct + (dec%8)*i;
  923.         dec = dec/8;
  924.         i = i*10;
  925.     }
  926. }
  927. }
  928.  
  929. case 12:
  930. {  char hex['9'];
  931.     char *hexstr;
  932.     int length = 0;
  933.     const int base = 16;
  934.     unsigned long  decnum = 0;
  935.     int i;
  936.        gotoxy(20,18); printf("\n\t\tEnter Hexadecimal Number up to 10 digits: ");
  937.     scanf("%s", hex);
  938.     for(hexstr = hex; *hexstr != '\0'; hexstr++)
  939.     {
  940.     length++;
  941.     }
  942.  
  943.     hexstr = hex;
  944.     for(i = 0; *hexstr != '\0' && i < length; i++, hexstr++)
  945.     {
  946.  
  947.     if(*hexstr >= 48 && *hexstr <= 57)
  948.     {
  949.         decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);
  950.     }
  951.     else if((*hexstr >= 65 && *hexstr <= 70))
  952.     {
  953.         decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);
  954.     }
  955.     else if(*hexstr >= 97 && *hexstr <= 102)
  956.     {
  957.         decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);
  958.     }
  959.     else
  960.     {
  961.       gotoxy(20,19);  printf("\n\t\tInvalid Hexadecimal Number \n");
  962.  
  963.     }
  964.     }
  965.  gotoxy(20,19);printf("\n\t\tEquivalent Decimal Number is %u  \n", decnum);
  966.  goto again;
  967. }
  968.  
  969. default:
  970. {
  971.     gotoxy(20,18);p("invalid input");
  972.     goto again;
  973. }
  974.  
  975. }
  976.         }
  977. char ans;
  978. again:
  979.  
  980.     p("\n\t\tWould you like to convert again? \n");
  981.         p("\n\t\tEnter y if Yes, Enter n if No: ");
  982.     s("\n\t\t%s",&ans);
  983.     switch(ans)
  984.     {
  985.     case 'y':case 'Y':
  986.         {
  987.             system("cls");
  988.  
  989.         goto ulit;
  990.         }
  991.     case 'n':case 'N':
  992.         {
  993.             system("cls");
  994.             p("\n\t\tTHANK YOU!\n");
  995.             p("\n\t\tPlease type any key to exit\n");
  996.             break;
  997.         }
  998.     default:
  999.         {
  1000.             p("\n\t\tPlease enter correct choice\n");
  1001.             goto again;
  1002.         }
  1003.     }
  1004.     break;
  1005.         }
  1006.     else
  1007.  
  1008.         {
  1009.             system("cls");
  1010.             printf("\n\t\tPassword Incorrect\n");
  1011.         }
  1012. }
  1013.     else
  1014.  
  1015.     {
  1016.         system("cls");
  1017.         printf("\n\t\tUsername Incorrect\n");
  1018.     }
  1019.     i--;
  1020. }while(i>=0);
  1021. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement