Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MAX_BUFFER_CAP 1024
- #include <string.h>
- #include <stdio.h>
- void decToBin(int);
- void decToOct(int);
- void decToHex(int);
- int main(void)
- {
- int num, choice;char ch;
- clrscr();
- do
- {
- ch = 'N';
- printf("\nEnter a number in base 10: ");
- scanf("%d", &num);
- printf("Which base do you want to convert it to?\n");
- printf("Press 1 for Binary\n");
- printf("Press 2 for Octal\n");
- printf("Press 3 for Hexadecimal\n\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch(choice)
- {
- case 1: printf("%d in binary is ", num);
- decToBin(num);
- break;
- case 2: printf("%d in octal is ", num);
- decToOct(num);
- break;
- case 3: printf("%d in hexadecimal is ", num);
- decToHex(num);
- break;
- default:printf("You entered a wrong choice! Please try again!");
- ch = 'Y';
- }
- if(ch == 'N')
- {
- printf("\n\nDo you want to continue (Y/N)? ");
- ch = getche();
- }
- }
- while(toupper(ch) == 'Y');
- getch();
- return 0;
- }
- void decToBin(int num)
- {
- char bin[MAX_BUFFER_CAP];
- char temp[MAX_BUFFER_CAP] = "";
- while(num > 0)
- {
- sprintf(bin, "%d%s", num%2, temp);
- strcpy(temp, bin);
- num /= 2;
- }
- printf("%s", bin);
- }
- void decToOct(int num)
- {
- char oct[MAX_BUFFER_CAP];
- char temp[MAX_BUFFER_CAP] = "";
- while(num > 0)
- {
- sprintf(oct, "%d%s", num%8, temp);
- strcpy(temp, oct);
- num /= 8;
- }
- printf("%s", oct);
- }
- void decToHex(int num)
- {
- char hex[MAX_BUFFER_CAP];
- char temp[MAX_BUFFER_CAP] = "";
- while(num > 0)
- {
- int dig = num%16;
- char ch;
- if(dig < 10)
- {
- ch = '0'+ dig;
- }
- else
- {
- ch = 'A' + (dig - 10);
- }
- sprintf(hex, "%c%s", ch, temp);
- strcpy(temp, hex);
- num /= 16;
- }
- printf("%s", hex);
- }
Add Comment
Please, Sign In to add comment