I was given this program to convert a inputted decimal number to a string which is binary number. Now this is a correct program and does work. I have a doubt in while
loop the expression saying ch = (rem + 48);
I believe value of remainder rem
is ultimately going to be inherited by ch
then why adding 0 ie. 48 in ascii terms is making difference. what is making addition of 48 convert rem
to character form. If I just write ch = rem;
, ch
is not considered as character and adding 48 makes it charcter. But Why???
void main()
{
char x[15],tmp,ch;
int i=0,j=0,dno,rem;
printf("\nEnter decimal number:");
scanf("%d",&dno);
while(dno>0)
{
rem = dno % 2;
ch = (rem+48);
x[j++] = ch;
dno/=2;
}
x[j--]='\0';
while(i<j)
{
tmp = x[j];
x[j--] = x[i];
x[i++] = tmp;
}
puts(x);
return;
}