Write a program sum of the two numbers in hexadecimal. that reads in two hexadec
ID: 3586097 • Letter: W
Question
Write a program sum of the two numbers in hexadecimal. that reads in two hexadecimal numbers from a file, hex.ext, and prints out the From Wikipedia: "In mathematics and computer science, hexadec hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to represent val and A, B, C, D,E, F (or alternatively a-f) ues zero to nine, to represent values ten to fifteen AF3 is equal, in decimal, to THE SUM OF: For example, the hexadecimal number 2 (2x 16') + (10 x 162) + (15 x 16') + (3x ie) og 10,995." For example, if the file contains: 45AF 12B3 ...and then your program will output (if you output the result in decimal): sten (Haxt) The decimal sum of 45AF and 12B3 is 22626. (To check your results, you can go to a hexadecimal calculator on the Web. For example, http://www.csgnetwork.com/hexaddsubcalc.html) To solve this problem: Read the hexadecimal numbers as character arrays Convert the charac character array as a parameter, and returns an integer) Add the numbers to get a decimal sum EXTRA CREDIT: convert the sum to hexadecimal (by calling a function that fills a) b) ter arrays to numbers (by calling a function that takes the c) d) a character array) PART II Assume that your file has an unknown number of hexadecimals. Modify/ Enhance/Change the program so that it prints the sum of all the numbers in the file.Explanation / Answer
Please see the following code.
Answer:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
int i ;
int c[30];
int x=0,y=1,j=0,k=0;
char s[30],q[30];
printf("Convert from hex to Dec");
printf("Enter 1st hex number");
scanf("%s",s);
printf("Enter 2nd hex number");
scanf("%s",q);
printf("Decimal Equivalent of hex=");
printf("Sum = ");
for(i=0;s[i]!=NULL; i++)
{
switch(s[i])
{
case '0':
c[i]=0;
break;
case '1':
c[i]=1;
break;
case '2':
c[i]=2;
break;
case '3':
c[i]=3;
break;
case '4':
c[i]=4;
break;
case '5':
c[i]=5;
break;
case '6':
c[i]=6;
break;
case '7':
c[i]=7;
break;
case '8':
c[i]=8;
break;
case '9':
c[i]=9;
break;
case 'a':
case 'A':
c[i]=10;
break;
case 'b':
case 'B':
c[i]=11;
break;
case 'c':
case 'C':
c[i]=12;
break;
case 'd':
case 'D':
c[i]=13;
break;
case 'e':
case 'E':
c[i]=14;
break;
case 'f':
case 'F':
c[i]=15;
break;
default:
printf(" Not a hexadecimal number");
break;
}
}
i--;
for(;i>=0;i--)
{
x=x+c[i]*y;
y=y*16;
j=j+c[i]*y;
k=(x+j)/8.5;
}
printf("%d ",k);
printf("%d ",(x+j));
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.