Convert your ID (eight digit numbers 0-9)from decimal to hexadecimal. Your answe
ID: 3662276 • Letter: C
Question
Convert your ID (eight digit numbers 0-9)from decimal to hexadecimal. Your answer should start with 0x and the rest should contain only digits and the letters A-F (upper case). For example 01359321. Write a C program, convert2tenandhex.c that takes one command line parameter. The parameter is an integer either in decimal or hexadecimal. If it is in hexadecimal, it starts with a leading 0x. The program outputs exactly 2 lines. The first line contains only your name. If the program has exactly one command line parameters, the second line contains two tokens separated by a single blank. The first token is the value of the parameter in decimal and the second token is the value of the parameter in hexadecimal (without the leading 0x). If the number of command line parameters is not correct, the second line is an appropriate error message. Run the program three times, once with no command line parameters, once with your ID and once with its hexadecimal equivalent (with he leading 0x). Put both the input and output for the three runs in a file called assign0-3.out. Make sure each run is clearly labeled. You may assume that the value specified on the command line will fit in an int.
Explanation / Answer
#include<stdio.h>
int main()
{
long int decimalNumber,remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];
printf("Enter decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0)
{
temp = quotient % 16;
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%c",hexadecimalNumber[j]);
return 0;
}