- #1
ParticleGinger6
- 32
- 5
- Homework Statement
- Your program should convert an integer into another base. The input integer could be in decimal, octal or hexadecimal. The output could be converted into any of the bases. A sample run is below. The user’s response is in boldface.
Base of input integer: Enter d for decimal, h for hexadecimal or o for octal: d
Enter the number: 178
Enter the base of the output (d, h or o): h
The integer 178 in decimal is b2 in hexadecimal
Do you wish to do another? (Y or N): Y
Base of input integer: Enter d for decimal, h for hexadecimal or o for octal: o
Enter the number: 423
Enter the base of the output: d
The integer 423 in octal is 275 in decimal
Do you wish to do another? (Y or N): N
- Relevant Equations
- Dec to Hex/Oct: divide by the wanted base till the remainder is greater than the dividen.
We have been requested to run this on linux. I am able to read in the original base system and then I am able to enter the number. As soon as I enter the original number the system reads out "Enter the base of the output:" then does not allow the user to enter the second base. Instead it just prints "Do you wish to do another". I have a scanf running for the second base however, it seems to not be wanting to scan anything in.
C:
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
int num, res, j, k;
long long int n;
char base1, base2, hex[100];
char ch;
do{
printf("\nBase of input integer: ");
printf("\nEnter d for decimal, h for hexadecimal or o for octal: ");
scanf("%c", &base1);
switch(base1){
case 'd': printf("\nEnter the number: ");
scanf("%d", &num);
printf("\nEnter the base of the output (d,h,o): ");
scanf("%c", &base2);
switch(base2){
case 'o': printf("\nOctal Value: ");
decconversion(num, 8);
printf("The decimal value %d in Octal value is %d",num,remainder);
break;
case 'h': printf("\nHexadecimal value: ");
decconversion(num, 16);
break;
}
break;
case 'h': printf("\nEnter the number: ");
scanf("%s", hex);
printf("Enter the base of the output (d,h,o): ");
scanf("%c", &base2);
switch(base2){
case 'd': printf("\nDecimal Value: %d",HexToDec(hex));
break;
case 'o': printf("\nOctal Value: %d", HexToOct(hex));
break;
}
case 'o': printf("\nEnter the number: ");
scanf("%lld", &n);
printf("\nEnter the base of the output (d,h,o): ");
scanf("%c", &base2);
switch(base2){
case 'd': printf("\nDecimal Value: %d", OctToDec(n));
break;
case 'h': printf("\nHexadecimal Value: %d", decconversion((OctToDec(n)), 16));
break;
}
break;
break;
}
printf("\nDo you wish to do another Y/N: ");
scanf("%c", &ch);
}
while(ch=='y' || ch == 'Y');
return 0;
}
int decconversion(int num, int base){
int remainder = num%base;
if(num==0){
return;
}
decconversion(num/base,base);
if(remainder <10){
printf("%d", remainder);
}
else {
printf("%c", remainder - 10+'A');
}
}
int HexToOct(char hex[]){
int i, len, dec = 0, oct =0;
for(len=0;hex[len]!='\0'; len++);
for(i=0; hex[ i]!='\0'; i++, len--){
if(hex[ i]>='0' & hex[ i]<='9'){
dec =dec+(hex[ i]-87)*pow(16, len-1);
}
}
i =1;
while(dec!=0){
oct = oct +(dec%8)*i;
dec = dec/8;
i = i*10;
}
return oct;
}
int HexToDec(char* hex){
int dec =0, base = 1, i, d;
char c;
i = strlen(hex)-1;
while(i>=0){
c = *(hex+i);
if(c>='A' && c<='F')
d = 10+c-'A';
else if(c>='a' && c<='f')
d = 10+c-'a';
else
d = c-'0';
dec +=d*base;
base*=16;
i--;
}
return dec;
}
int OctToDec(int oct){
int dec = 0, base =1;
int t;
t = oct;
while(t>0){
dec+=(t%10)*base;
t/=10;
base*=8;
}
return dec;
}
Last edited by a moderator: