Please answer this

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhuvanaa
    New Member
    • Sep 2006
    • 1

    Please answer this

    i want to add 2 four digit numbers using modulo 10 addition. One of the number will going to get using Random function(only 4 digits). I want to know how to get the random number and perform Modulo 10 Addition.

    1234
    6789 (this number is created by random function)
    --------------
    7913
    ------------
  • risby
    New Member
    • Sep 2006
    • 30

    #2
    Originally posted by bhuvanaa
    i want to add 2 four digit numbers using modulo 10 addition. One of the number will going to get using Random function(only 4 digits). I want to know how to get the random number and perform Modulo 10 Addition.

    1234
    6789 (this number is created by random function)
    --------------
    7913
    ------------
    the c library includes functions rand and srand. use srand to set the seed of a sequence of psuedo-random numbers and rand to get the next value from the sequence.

    modulo 10 arithmetic means that when you add two digits if the result is greater than 9 subtract 10. From your example above: 4 + 9 = 3 and 8 + 3 = 1.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      You can use the % operator to help you with modulo arithmatic, it is the modulo operator, it gives the remainder when 1 integer is divided by another

      4 + 9 = 13

      (4 + 9) % 10 = 3

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        Addition/subtraction without carry is the same thing as XOR. However, since XOR and computer arithmetic is in binary, it doesn't quite churn out the same result for decimal. However, if you used a base like 8 or 16 (or any power of two) it should come out correct.

        Comment

        Working...