# Addition of two hex numbers program :(

• 11-15-2012
yamanin403
Addition of two hex numbers program :(
hi i'm a newbie to C programming and got a homework that I couldn't even start...

It's about making a program that gets to hex numbers, add them and show the results.

It should look like

Enter the first number:
Enter the second number:

number 1
+
number 2
---------
sum

the thing is the results should be aligned to the right
and the number of '-' in the results should equal to the largest number of digits...

For example,

first number: A444444
second number: B1234567
A444444
+
B1234567
(7 '-') => --------
result

and the results should only contain capital letters
Can anyone help me making a program like this?

Thanks a lot....
• 11-15-2012
twiki
Quote:

Originally Posted by yamanin403
hi i'm a newbie to C programming and got a homework that I couldn't even start...

It's about making a program that gets to hex numbers, add them and show the results.

It should look like

Enter the first number:
Enter the second number:

number 1
+
number 2
---------
sum

the thing is the results should be aligned to the right
and the number of '-' in the results should equal to the largest number of digits...

For example,

first number: A444444
second number: B1234567
A444444
+
B1234567
(7 '-') => --------
result

and the results should only contain capital letters
Can anyone help me making a program like this?

Thanks a lot....

look into the scanf function... below is the page describing it and all it's formatting options
scanf(3) - Linux manual page

for example... assuming number is defined like so: int number;
then to read from standard input a hexadecimal number into "number" you would use the following

scanf("%x",&number);

And to simply display the results, all you have to do is read into two different variables using the method I described above and use the printf function to display the results like so:

printf("The result is %8x\n", first_num + second_num);

where first_num and second_num were declared as int in your program

**EDIT**

I re-read your post and I thought I would point out that in the printf statement above you can replace the lowercase 'x' in the format string with an uppercase 'X' and the results will be displayed in uppercase letters only...

e.g. printf("The result is %8X\n",first_num + second_num);

this will print only uppercase hex digits.