hi eveRyone,
I just want to have your tips for my code..

``````#include <stdio.h>

main()
{
hexa = 16;
printf("enter decimal number: ");

scanf("%d",&deci);

for (ctr = 1; ctr<=deci; ctr++)

quotient = deci / hexa;

getche();
}``````

I want to print out the numbers 10-16 in letters like this, 10=A,11=B,12=C,13=D,14=E,15=F,16=G..but i dont know how to make it coz im new with C..

## All 15 Replies

the easiest way is to use sprintf with "%X" format specifier

``````int main()
{
char buf = {0};
int x = 1234;
sprintf(buf,"%X", x);
printf("%s\n", buf);
return 0;
}``````

wow! thanks a lot mr.,but still nothing happened..

You made me laugh with your post to convert decimal to hex, including "A,B...G"
*G* ?? :D

Anyway, your algorithm to change decimal to hexadecimal is wrong. Here's a modded version of your program, which shows what the hex value of a decimal, should be:

``````#include <stdio.h>

int main(void)
{
int hexa = 16, deci, quotient, ctr;
printf("enter decimal number: ");
scanf("%d",&deci);

for (ctr = 1; ctr<=deci; ctr++)
quotient = deci / hexa;

printf("\n\n Try this for Hexadecimal: %X", deci);

getche();
return 0;
}

thanKs duDe!

Not to be nitpicky, but getche() isn't standard C. You should use getchar() instead.

And here's a link about scanf() (or did I give you it before?)

Thank you very much dude, I appreciate that!

Can you help me with my prob?
here's my code..

``````#include <stdio.h>

int main(void)  {

int ctr, bin, quotient, deci=0, binary = 2;
float rem;
char mark_magic;

gotoxy(28,9);printf("enter decimal number: ");

scanf("%d",&deci);
quotient = deci / binary;

printf("Equivalent in Binary is ");

for(bin=0; bin <=3; bin++)
{
printf("%d",deci % 2);
deci = deci / 2;
}

getchar();
}``````

I want to print it out in binary, but the answer is being written out in reverse.

Did you learn about arrays yet? You could store everything in an array and then print them out in reverse order.
You could also change the calculation. Look into the pow() function

``````void printB( int num )
{
if( num == 0 )
return;
else
{
printB( num / 2 );
printf("%d", num % 2 );
}
}``````

You could solve the problem of you binary values printing in reverse using the recursive function. Have a look the function above. You could see the before the function is called again it pushes the "num % 2" to the stack and when it return it print the values which is on top of the static that is "0%2" ........ "num%2".

Call the function as follow:

``````printB( 23 );

/* my output
10111
*/``````

NOTE: I wonder this would work for a long decimal number, due to overflow!

You might as well, look into some concept of code indendation, that code which you have posted has a horriable indendation! And it also give a me a nasty taste of you using a very old Turbo C compile??? Start looking at some better compiler. DEV-C++, Code::block

ssharish

Thanks everyone, I finally got it right!

Why should we worry about converting the number to hex or binary. Its already stored in binary format.

Well converting hex to binary is just problem. But there is one more reason. It is difficult to remember the binary pattern. So we group them to form it small number which still referees the same binary patten. There no any reason on why do we have to convert it binary to hex. At at least i havn;t come across any application which need a that feature!

ssharish

There no any reason on why do we have to convert it binary to hex. At at least i havn;t come across any application which need a that feature!

I have.

commented: same here ... +4
``````char a[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
void dectohexa(int val)
{
if(val == 0) return;
dectohexa(val / 16);
printf("%c", a[val%16]);

}
``````

Try dis :

Program to convert input decimal value to its hexadecimal equivalent

``````#include <stdio.h>
#include <conio.h>
#include <math.h>
void dtoh(int d);
void main()
{
int d;
clrscr();
printf("Enter a no. in decimal system:-  ");
scanf("%d",&d);
dtoh(d);
getch();
}

void dtoh(int d)
{
int b,c=0,a,i=0;
b=d;
while (b>15)
{
a[i]=b%16;
b=b/16;
i++;
c++;
}
a[i]=b;
for (i=c;i>=0;--i)
{
if (a[i]==10)
printf("A");
else if (a[i]==11)
printf("B");
else if (a[i]==12)
printf("C");
else if (a[i]==13)
printf("D");
else if (a[i]==14)
printf("E");
else if (a[i]==15)
printf("F");
else
printf("%d",a[i]);
}
return;
}``````
commented: 3 years late, bad non-standard code, bad formatting, no CODE tags. Good job! -4
``````//Decimal to Hex value
main()
{
char hex={"0123456789ABCDEF"};
int num,i=0,j;
char result[];
printf("Enter the Decimal No.\n");
scanf("%d",&num);
while(num)
{
result[i]=hex[num%16];
num=num=num/16;
i++;
}
printf("Given num of the hex value is:");
for(j=i-1;j>=0;j--)
printf("%c",result[j]);
getch();
return 0;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.