I need a program that: Can convert any number in a specific base and convert it in another base. It must work for real numbers (e.g 7.34 , 3.14, etc.)
Input: number1, base1, base_to_which_it_must_be_converted Output: Converted number:
I managed to write such programs, but they wont work for real numbers.
#include <stdio.h>
#include <string.h>
char reVal(int num)
{
if (num >= 0 && num <= 9)
return (char)(num + '0');
else
return (char)(num - 10 + 'A');
}
void strev(char *str)
{
int len = strlen(str);
int i;
for (i = 0; i < len/2; i++)
{
char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
}
char* fromDeci(char res[], int base, int inputNum)
{
int index = 0;
while (inputNum > 0)
{
res[index++] = reVal(inputNum % base);
inputNum /= base;
}
res[index] = '\0';
strev(res);
return res;
}
int main()
{
int inputNum = 10, base = 3;
char res[100];
printf("Equivalent of %d in base %d is "
" %s\n", inputNum, base, fromDeci(res, base, inputNum));
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int b,c,x,nr,nr2;
char z;
int main()
{
int num,i,l,b,a[50];
l=0;
printf("introduce the number and its base\n");
scanf("%d",&num);
scanf("%d",&b);
int n;
i=0;
n=num;
l=0;
while (n!=0)
{
l++;
n/=10;
}
n=num;
i=l;
while (n!=0)
{
a[i]= n%10;
n/=10;
i--;
}
i=0;
while(i<=l)
{
x=x*b+a[i];
i++;
}
printf("\n%d",x);
return 0;
}