Addition of two hex numbers program :(

This is a discussion on Addition of two hex numbers program :( within the C Programming forums, part of the General Programming Boards category; hi i'm a newbie to C programming and got a homework that I couldn't even start... It's about making a ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    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....

  2. #2
    Registered User
    Join Date
    Nov 2012
    Location
    Some rock floating in space...
    Posts
    32

    Smile

    Quote Originally Posted by yamanin403 View Post
    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);

    Please note the & address of operator before the variable 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.
    Last edited by twiki; 11-15-2012 at 07:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program using addition only
    By cowboyz209 in forum C Programming
    Replies: 3
    Last Post: 04-30-2011, 08:54 PM
  2. Bug in the addition drill program
    By Sharifhs in forum C Programming
    Replies: 1
    Last Post: 08-13-2010, 11:50 PM
  3. Addition of two numbers
    By saswatdash83 in forum C Programming
    Replies: 9
    Last Post: 06-11-2009, 12:51 PM
  4. Addition Program - Help
    By ggraz in forum C Programming
    Replies: 6
    Last Post: 10-15-2008, 10:45 PM
  5. Addition of Roman numbers
    By hacica in forum C Programming
    Replies: 12
    Last Post: 12-10-2007, 06:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21