Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I generate a number divisable by 5, and check it?

How do I generate a number divisable by 5, and check it?

Scheduled Pinned Locked Moved C#
questionlounge
79 Posts 34 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stephen darling
    wrote on last edited by
    #1

    Hi. First, how would I create a random number, and then add the last digit, so that it is divisable by 5? the number should always be 5 digits long. Second, how do I check it, I think I need to do something like...

    if (int x MOD 5 ==0)

    Or something like that. The first step is the most important though. Thank you, Steve

    L R D L T 13 Replies Last reply
    0
    • S stephen darling

      Hi. First, how would I create a random number, and then add the last digit, so that it is divisable by 5? the number should always be 5 digits long. Second, how do I check it, I think I need to do something like...

      if (int x MOD 5 ==0)

      Or something like that. The first step is the most important though. Thank you, Steve

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      a random multiple of five is bound to be five times some other random number. :omg:

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      S M F 3 Replies Last reply
      0
      • S stephen darling

        Hi. First, how would I create a random number, and then add the last digit, so that it is divisable by 5? the number should always be 5 digits long. Second, how do I check it, I think I need to do something like...

        if (int x MOD 5 ==0)

        Or something like that. The first step is the most important though. Thank you, Steve

        R Offline
        R Offline
        riced
        wrote on last edited by
        #3

        Generate a four digit random number, multiply it by 10, add 5 (or 10). The result will be divisible by 5.

        Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

        S S M 3 Replies Last reply
        0
        • S stephen darling

          Hi. First, how would I create a random number, and then add the last digit, so that it is divisable by 5? the number should always be 5 digits long. Second, how do I check it, I think I need to do something like...

          if (int x MOD 5 ==0)

          Or something like that. The first step is the most important though. Thank you, Steve

          D Offline
          D Offline
          David1987
          wrote on last edited by
          #4

          Generate a random integer in [2000, 19999] and multiply it by 5. The result will always be in [10000, 99999] (ie 5 decimal digits) and be divisible by 5 (by construction)

          int yourNumber = 5 * rand.Next(2000, 20000); // remember the max-bound is exclusive
          if (yourNumber % 5 != 0)
          Console.WriteLine("the universe is wrong");

          And make sure you reuse a single instance of Random, if you create new ones the result won't be random. [/spoon feeding]

          S 1 Reply Last reply
          0
          • R riced

            Generate a four digit random number, multiply it by 10, add 5 (or 10). The result will be divisible by 5.

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

            S Offline
            S Offline
            stephen darling
            wrote on last edited by
            #5

            Does this mean that no matter what number I generate, it will always have to end in 5? e.g. All these numbers are divisable by 5

            39485
            99045
            12095
            49385
            99335

            However, I was under the impression I could generate numbers that would be divisable by 5, but not end in 5? Is this wrong? Regards, Stephen

            D R G K R 5 Replies Last reply
            0
            • L Luc Pattyn

              a random multiple of five is bound to be five times some other random number. :omg:

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              S Offline
              S Offline
              stephen darling
              wrote on last edited by
              #6

              Luc Pattyn wrote:

              a random multiple of five is bound to be five times some other random number. :OMG:

              ? What do you mean? Regards, Stephen

              M 1 Reply Last reply
              0
              • S stephen darling

                Does this mean that no matter what number I generate, it will always have to end in 5? e.g. All these numbers are divisable by 5

                39485
                99045
                12095
                49385
                99335

                However, I was under the impression I could generate numbers that would be divisable by 5, but not end in 5? Is this wrong? Regards, Stephen

                D Offline
                D Offline
                David1987
                wrote on last edited by
                #7

                Numbers that end in 0 are also divisible by 5 (except zero) So you're right.

                L K 2 Replies Last reply
                0
                • S stephen darling

                  Does this mean that no matter what number I generate, it will always have to end in 5? e.g. All these numbers are divisable by 5

                  39485
                  99045
                  12095
                  49385
                  99335

                  However, I was under the impression I could generate numbers that would be divisable by 5, but not end in 5? Is this wrong? Regards, Stephen

                  R Offline
                  R Offline
                  riced
                  wrote on last edited by
                  #8

                  Look at what I said - add 5 or 10. If a number is divisible by 5 it must end in 5 or 0 - that's primary school arithmetic. If you don't believe me write out the 5 times table for the numbers 1 to 20.

                  Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

                  S K 2 Replies Last reply
                  0
                  • D David1987

                    Numbers that end in 0 are also divisible by 5 (except zero) So you're right.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    David1987 wrote:

                    except zero

                    :confused::confused::confused:

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    D 1 Reply Last reply
                    0
                    • S stephen darling

                      Luc Pattyn wrote:

                      a random multiple of five is bound to be five times some other random number. :OMG:

                      ? What do you mean? Regards, Stephen

                      M Offline
                      M Offline
                      Manfred Rudolf Bihy
                      wrote on last edited by
                      #10

                      What Luc meant was that you should just create a random integer number and the go and multiply it by five. This will always give you a number which is divisible by 5, to state the obvious. The only thing I would add is if you are given a range in which the numbers should lie you'd need some adjustment.

                      Random rnd = new Random();

                      int lowerBound = 2001; // not divisable 5 five
                      lowerBound = lowerBound + ( 5 - lowerBound % 5 ); // make lowerBound divisable by five

                      int upperBound = 10023; // not divisable by five
                      upperBound = upperBound - ( upperBound % 5 ); // make upperBound divisable by five

                      int range = (upperBound - lowerBound) / 5; // calculate the range of numbers

                      int number = lowerBound + rnd.Next( range ) * 5; // presto

                      Cheers!

                      —MRB

                      "With sufficient thrust, pigs fly just fine."

                      Ross Callon, The Twelve Networking Truths, RFC1925

                      G 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        a random multiple of five is bound to be five times some other random number. :omg:

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        M Offline
                        M Offline
                        Manfred Rudolf Bihy
                        wrote on last edited by
                        #11

                        Stating the obvious! 5+ :-D :thumbsup:

                        "With sufficient thrust, pigs fly just fine."

                        Ross Callon, The Twelve Networking Truths, RFC1925

                        1 Reply Last reply
                        0
                        • R riced

                          Look at what I said - add 5 or 10. If a number is divisible by 5 it must end in 5 or 0 - that's primary school arithmetic. If you don't believe me write out the 5 times table for the numbers 1 to 20.

                          Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

                          S Offline
                          S Offline
                          stephen darling
                          wrote on last edited by
                          #12

                          riced wrote:

                          If a number is divisible by 5 it must end in 5 or 0

                          Yes; I was getting confused with the coding issue not the actual math.

                          riced wrote:

                          that's primary school arithmetic.

                          I know, I am a Biomedical Scientist.

                          riced wrote:

                          If you don't believe me write out the 5 times table for the numbers 1 to 20.

                          I do believe you. Like I said, it was the coding side of things. However, it is my fault the way I explained myself, it did indeed look as though I didn’t understand the math itself, and I certainly was not questioning you answer. Sorry if it came across that way, and thank you. Kind Regards, Stephen

                          M M F 3 Replies Last reply
                          0
                          • L Luc Pattyn

                            David1987 wrote:

                            except zero

                            :confused::confused::confused:

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            D Offline
                            D Offline
                            David1987
                            wrote on last edited by
                            #13

                            It depends on the definition of divisibility that you use. Zero can also be divisible by anything, if you use an other definition.

                            L 1 Reply Last reply
                            0
                            • R riced

                              Generate a four digit random number, multiply it by 10, add 5 (or 10). The result will be divisible by 5.

                              Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

                              S Offline
                              S Offline
                              Simon Bang Terkildsen
                              wrote on last edited by
                              #14

                              you shouldn't add 5 or 10 but 5 or 0. In the case the random generator provides 9999 then you would get 9999 * 10 + 10 = 99990 + 10 = 100000 six digits

                              1 Reply Last reply
                              0
                              • D David1987

                                It depends on the definition of divisibility that you use. Zero can also be divisible by anything, if you use an other definition.

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #15

                                BS.

                                zero times x equals zero, no matter what (finite) value x has.

                                so (the right side's) zero is divisible by x, and the result is (the left side's) zero. If I hold 10 pies, 5 bacon sandwiches, and zero glasses of milk, I have no problem distributing them evenly to 5 people. Next you'll state you could also redefine 5, so it no longer divides itself. :)

                                Luc Pattyn [My Articles] Nil Volentibus Arduum

                                OriginalGriffO D 2 Replies Last reply
                                0
                                • L Luc Pattyn

                                  BS.

                                  zero times x equals zero, no matter what (finite) value x has.

                                  so (the right side's) zero is divisible by x, and the result is (the left side's) zero. If I hold 10 pies, 5 bacon sandwiches, and zero glasses of milk, I have no problem distributing them evenly to 5 people. Next you'll state you could also redefine 5, so it no longer divides itself. :)

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                                  OriginalGriffO Offline
                                  OriginalGriffO Offline
                                  OriginalGriff
                                  wrote on last edited by
                                  #16

                                  Luc Pattyn wrote:

                                  Next you'll state you could also redefine 5, so it no longer divides itself.

                                  It doesn't. There are only four bacon sandwiches left... :laugh:

                                  Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                  L 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    BS.

                                    zero times x equals zero, no matter what (finite) value x has.

                                    so (the right side's) zero is divisible by x, and the result is (the left side's) zero. If I hold 10 pies, 5 bacon sandwiches, and zero glasses of milk, I have no problem distributing them evenly to 5 people. Next you'll state you could also redefine 5, so it no longer divides itself. :)

                                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                                    D Offline
                                    D Offline
                                    David1987
                                    wrote on last edited by
                                    #17

                                    Nope. You seem to think that that is the only definition of divisibility. I did not personally redefine anything. There is no natural number n such that 0/x=n so no x evenly divides 0. If you use the definition with integers instead of natural numbers, everything divides zero. Also, the prime factorization of zero is empty.

                                    L B A 3 Replies Last reply
                                    0
                                    • S stephen darling

                                      Hi. First, how would I create a random number, and then add the last digit, so that it is divisable by 5? the number should always be 5 digits long. Second, how do I check it, I think I need to do something like...

                                      if (int x MOD 5 ==0)

                                      Or something like that. The first step is the most important though. Thank you, Steve

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #18

                                      Didn't you already post this in the Q&A and get an answer to it?? Generating numbers to the multiple of 5?[^]

                                      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                                      1 Reply Last reply
                                      0
                                      • D David1987

                                        Nope. You seem to think that that is the only definition of divisibility. I did not personally redefine anything. There is no natural number n such that 0/x=n so no x evenly divides 0. If you use the definition with integers instead of natural numbers, everything divides zero. Also, the prime factorization of zero is empty.

                                        L Offline
                                        L Offline
                                        Luc Pattyn
                                        wrote on last edited by
                                        #19

                                        natural numbers are the ordinary counting numbers 1, 2, 3, ... (sometimes zero is also included) is what Wikipedia[^] offers as a definition. Now you can choose: either you include zero and you are allowed to use it at both sides of your 0/x=n, or you exclude it (and then your "except zero" remark that started all this is completely irrelevant). :doh:

                                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                                        D 1 Reply Last reply
                                        0
                                        • OriginalGriffO OriginalGriff

                                          Luc Pattyn wrote:

                                          Next you'll state you could also redefine 5, so it no longer divides itself.

                                          It doesn't. There are only four bacon sandwiches left... :laugh:

                                          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                                          L Offline
                                          L Offline
                                          Luc Pattyn
                                          wrote on last edited by
                                          #20

                                          Sorry for the late reply, I have been off-line this evening, I have another tournament going on this week. I trust all bacon sandwiches have magically disappeared by now, and so the problem got solved? :)

                                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups