I have created a C program for all number conversion, its a small application using C language. I have used printf’s various format specifiers to minimize the code and utilizing the available features of printf function.
These are some of format specifiers used in the code.
- %d => for signed decimal integer.
- %o => unsigned octal integer.
- %X => unsigned hexadecimal integer.
The number conversions supported by this code are listed below.
- Binary to Octal
- Binary to Decimal
- Binary to Hexadecimal
- Octal to Binary
- Octal to Decimal
- Octal to Hexadecimal
- Decimal to Binary
- Decimal to Octal
- Decimal to Hexadecimal
- Hexadecimal to Binary
- Hexadecimal to Octal
- Hexadecimal to Decimal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
#include< conio.h> #include< stdio.h> #include< math.h> #include< string.h> #define toBinary 1 #define toOctal 2 #define toDecimal 3 #define toHexadecimal 4 // Function to show the Converted number void showConvertedNumber(int choice, int num){ int i=0,sizeofInt; switch(choice){ case toBinary : sizeofInt = sizeof(int)*8; // To take size of int in bits printf("####### Binary : "); for(i=0;i < sizeofInt; i++) printf("%d",(num << i & 1 << (sizeofInt-1)) ? 1 : 0); break; case toOctal : printf("####### Octal : %o",num); break; case toDecimal : printf("####### Decimal : %d",num); break; case toHexadecimal : printf("####### Hexadecimal : %X",num); break; } getch(); } // Function to show the error message void showError(){ printf("####### Enter valid number! "); getch(); } // Function to convert binary number to decimal int getDecimal(char *binary){ int i,decimal=0; for(i = strlen(binary)-1; i >= 0 ;i--){ if(binary[i] == '1') decimal += (int) pow((double) 2, (double) (strlen(binary)-1 - i)); } return decimal; } // Function to validate the binary string. int validateBinary(char *binary){ int valid = 1,i; for(i = 0 ; i < strlen(binary); i++){ if(binary[i] == '1' || binary[i] == '0') continue; else{ valid = 0; break; } } return valid; } void main(){ int choice,num,innerChoice,valid; char binary[sizeof(int)]; textmode(C40); textbackground(WHITE); textcolor(BLUE); do{ clrscr(); printf("####### 799 Web Nixar's Creation ####"); printf("________________________________________"); printf("####### Number Conversion Menu ####"); printf("========================================"); printf("====> 1.Binary to Octal ####"); printf("====> 2.Binary to Decimal ####"); printf("====> 3.Binary to Hexadecimal ####"); printf("________________________________________"); printf("====> 4.Octal to Binary ####"); printf("====> 5.Octal to Decimal ####"); printf("====> 6.Octal to Hexadecimal ####"); printf("________________________________________"); printf("====> 7.Decimal to Binary ####"); printf("====> 8.Decimal to Octal ####"); printf("====> 9.Decimal to Hexadecimal ####"); printf("________________________________________"); printf("====> 10.Hexadecimal to Binary ####"); printf("====> 11.Hexadecimal to Octal ####"); printf("====> 12.Hexadecimal to Decimal ####"); printf("________________________________________"); printf("====> 13.Exit ####"); printf("========================================"); printf("####### Enter your Choice : "); scanf("%d",&choice); switch(choice){ case 1 : case 2 : case 3 : innerChoice = (choice == 1 ? 2 : choice == 2 ? 3 : 4); printf("####### Enter binary number : "); scanf("%s",binary); // validation valid = validateBinary(binary); if(0 == valid){ showError(); break; } num = getDecimal(binary); showConvertedNumber(innerChoice,num); break; case 4 : case 5 : case 6 : innerChoice = (choice == 4 ? 1 : choice == 5 ? 3 : 4); printf("####### Enter octal number : "); valid = scanf("%o",&num); // validation if(0 == valid){ showError(); break; } showConvertedNumber(innerChoice,num); break; case 7 : case 8 : case 9 : innerChoice = (choice == 7 ? 1 : choice == 8 ? 2 : 4); printf("####### Enter decimal number : "); valid = scanf("%d",&num); // validation if(0 == valid){ showError(); break; } showConvertedNumber(innerChoice,num); break; break; case 10: case 11: case 12: innerChoice = (choice == 10 ? 1 : choice == 11 ? 2 : 3); printf("####### Enter Hexadecimal number : "); valid = scanf("%x",&num); // validation if(0 == valid){ showError(); break; } showConvertedNumber(innerChoice,num); break; case 13: exit(1); break; default: printf("####### Enter valid choice "); getch(); main(); break; } }while(1); getch(); } |