In: Computer Science
Write a program that read the input from user and convert it to binary and hex in C language.
Thanks for the question.
Here is the completed code for this problem.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer. Thanks
===========================================================================
#include<stdio.h>
int main(){
int integer;
// array to store binary number
int bin[32];
// array to store hexadecimal characters
int hex[32];
printf("Enter an integer: ");scanf("%d",&integer);
int i = 0;
int n=integer;
while (n > 0) {
// storing remainder in binary array
bin[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
printf("%d",bin[j]);
n=integer;
i=0;int remainder;
while(n>0){
// storing remainder in hex array
hex[i] = n % 16;
n = n / 16;
i++;
}
printf("\n\n");
// printing hex array in reverse order
for (int j = i - 1; j >= 0; j--)
if(0<=hex[j] &&
hex[j<10]<10)printf("%d",hex[j]);
else if(hex[j]==10)printf("A");
else if(hex[j]==11)printf("B");
else if(hex[j]==12)printf("C");
else if(hex[j]==13)printf("D");
else if(hex[j]==14)printf("E");
else printf("F");
}