forked from ASCI-COEP/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/********************************************************** | ||
Program to convert a decimal number to binary, | ||
octal and hexadecimal using recursion | ||
* | ||
* Select conversion: | ||
|
||
* 1. Decimal to binary. | ||
* 2. Decimal to octal. | ||
* 3. Decimal to hexadecimal. | ||
* 4. Exit. | ||
|
||
* Enter your choice: 1 | ||
* Enter a number: 4 | ||
* Result = 100 | ||
**********************************************************/ | ||
|
||
#include<stdio.h> // include stdio.h library | ||
void convert_to_x_base(int, int); | ||
|
||
|
||
int main(void) | ||
{ | ||
int num, choice, base; | ||
|
||
while(1) | ||
{ | ||
printf("Select conversion: \n\n"); | ||
printf("1. Decimal to binary. \n"); | ||
printf("2. Decimal to octal. \n"); | ||
printf("3. Decimal to hexadecimal. \n"); | ||
printf("4. Exit. \n"); | ||
|
||
printf("\nEnter your choice: "); | ||
scanf("%d", &choice); | ||
|
||
switch(choice) | ||
{ | ||
case 1: | ||
base = 2; | ||
break; | ||
case 2: | ||
base = 8; | ||
break; | ||
case 3: | ||
base = 16; | ||
break; | ||
case 4: | ||
printf("Exiting ..."); | ||
exit(1); | ||
default: | ||
printf("Invalid choice.\n\n"); | ||
continue; | ||
} | ||
|
||
printf("Enter a number: "); | ||
scanf("%d", &num); | ||
|
||
printf("Result = "); | ||
|
||
convert_to_x_base(num, base); | ||
|
||
printf("\n\n"); | ||
} | ||
|
||
return 0; // return 0 to operating system | ||
} | ||
|
||
|
||
void convert_to_x_base(int num, int base) | ||
{ | ||
int rem; | ||
|
||
// base condition | ||
if (num == 0) | ||
{ | ||
return; | ||
} | ||
|
||
else | ||
{ | ||
rem = num % base; // get the rightmost digit | ||
convert_to_x_base(num/base, base); // recursive call | ||
if(base == 16 && rem >= 10) | ||
{ | ||
printf("%c", rem+55); | ||
} | ||
|
||
else | ||
{ | ||
printf("%d", rem); | ||
} | ||
} | ||
|
||
} |