In: Computer Science
C programming assignment.
instructions are given below and please edit this code only.
also include screenshot of the output
//In this assignment, we write code to convert decimal integers
into hexadecimal numbers
//We pratice using arrays in this assignment
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//convert the decimal integer d to hexadecimal, the result is
stored in hex[]
void dec_hex(int d, char hex[])
{
char digits[] ={'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
int k = 0;
//Fill in your code below
//It should not be hard to obtain the last digit of a
hex number
//Then what?
//If we are getting the digits in a reverse order,
what should we do in the end?
//Make sure the last character is a zero so that we
can print the string correctly
hex[k] = '\0';
}
// Do not change the code below
int main()
{
int d;
char hex[80];
printf("Enter a positive integer : ");
scanf("%d", &d);
dec_hex(d, hex);
printf("%s\n", hex);
return 0;
}
If you have any doubts, please give me comment...
//In this assignment, we write code to convert decimal integers into hexadecimal numbers
//We pratice using arrays in this assignment
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//convert the decimal integer d to hexadecimal, the result is stored in hex[]
void dec_hex(int d, char hex[])
{
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B','C', 'D', 'E', 'F'};
int k = 0;
while(d>0){
int rem = d%16;
hex[k] = digits[rem];
d = d/16;
k++;
}
//reversing
int i=0;
while(i<k/2){
char t = hex[i];
hex[i] = hex[k-i-1];
hex[k-i-1] = t;
i++;
}
hex[k] = '\0';
}
// Do not change the code below
int main()
{
int d;
char hex[80];
printf("Enter a positive integer : ");
scanf("%d", &d);
dec_hex(d, hex);
printf("%s\n", hex);
return 0;
}