Question

How can I convert decimal number 287 to hex by using loops? not using hex function

How can I convert decimal number 287 to hex by using loops? not using hex function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <string.h>

void ReverseDigits(char *arr)

{

int i = 0 , j = strlen(arr)-1;

while(i<j)

{

char temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

++i;

--j;

}

}

int main()

{

int d , r, i = 0, num;

printf("Enter a number: ");

scanf("%d", &d);

r = 16;

num = d;

char output[100];

while (d > 0)

{

int val = d % r;

if(val>=0 && val<=9)

output[i++] = (char)(val + '0');

else

output[i++] = (char)(val - 10 + 'A');

d = d/r;

}

output[i] = '\0';

ReverseDigits(output);

printf("The number %d in base %d is %s\n", num, r, output);

return 0;

}

====================================
SEE OUTPUT


Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
How can I convert decimal number 287 to hex by using loops? not using hex function
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT