Hi Guys,
Thank you for your replies. My ultimate goal is to write a program to convert the base from decimal to any base between 2 to 36. Here, the input is a decimal value and base in which the number is to be converted and the output is the converted number. The input value may contain characters.
I have written a program to achieve this, but on running this on dev-c++ forums, my program hangs after inputting the number and the base values. I don't see any error in my code, kindly suggest changes:
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int newbase,n,i,ascii,ndigit=0,q,r,zero=48;
char newrep[100];
printf("Enter the number to be converted");
scanf("%d",&n);
printf("Enter the base between 2 and 36 to which the number is to be converted");
scanf("%d",&newbase);
zero=atoi(0);
q=n;
ndigit=0;
while(q!=0)
{
r=q%newbase;
ndigit+=1;
ascii=zero+r;
if(ascii>atoi(9))
{
ascii=ascii+7;
}
newrep[ndigit]=(char)ascii;
q=q/newbase;
}
printf("Base %d representation of %d is",newbase,n);
for(i=ndigit;i<=1;i--)
{
scanf("%d",newrep[ndigit]);
}
printf("%d",newrep[ndigit]);
system("pause");
return 0;
}