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 stephen darling

    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 Offline
    M Offline
    Mycroft Holmes
    wrote on last edited by
    #21

    Your confusion did lead me to think - how old is this guy, doesn't understand primary grade maths :-D

    Never underestimate the power of human stupidity RAH

    1 Reply Last reply
    0
    • L Luc Pattyn

      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 Offline
      D Offline
      David1987
      wrote on last edited by
      #22

      Yes but that's precisely the point, you can choose.

      1 Reply Last reply
      0
      • D David1987

        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 Offline
        S Offline
        stephen darling
        wrote on last edited by
        #23

        David1987 wrote:

        And make sure you reuse a single instance of Random,

        How exactly do I ensure that I am doing this? I am using rand a number of times, and although I get different values, it does appear that they are very simular. Regards, Stephen

        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.

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #24

          That is a stupid definition. It would also indicate that -10 is not divisible by 5. There may be abstruse mathematical concepts for which it's useful, I suppose, but for normal maths it is nonsense.

          D 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

            G Offline
            G Offline
            Gary Wheeler
            wrote on last edited by
            #25

            All integers evenly divisible by 5 must end in either 5 or 0 when expressed in base 10. This is basic math.

            Software Zen: delete this;

            1 Reply 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

              T Offline
              T Offline
              Tom Chantler
              wrote on last edited by
              #26

              Is this a joke question? Generate a four digit number and add an extra number to the end, being either 5 or 0. Then don't check it because it will be correct!!! e.g. 4678; add 5 on the end to give 46785.

              M 1 Reply 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

                P Offline
                P Offline
                Paulo_JCG
                wrote on last edited by
                #27

                Random random = new Random(); return (random.Next(17999) + 2000) * 5; this gives a number divisable by 5 between 10000 and 99995

                Paulo Gomes Over and Out :D

                1 Reply Last reply
                0
                • S stephen darling

                  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 Offline
                  M Offline
                  mmwlada
                  wrote on last edited by
                  #28

                  stephen.darling wrote:

                  riced wrote:

                  that's primary school arithmetic.

                  I know, I am a Biomedical Scientist.

                  You are NOT a scientist. A scinetist knows elementary math. You should be ashamed of yourself.

                  There can be only one.

                  S R N 3 Replies Last reply
                  0
                  • M mmwlada

                    stephen.darling wrote:

                    riced wrote:

                    that's primary school arithmetic.

                    I know, I am a Biomedical Scientist.

                    You are NOT a scientist. A scinetist knows elementary math. You should be ashamed of yourself.

                    There can be only one.

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

                    mmwlada wrote:

                    You are NOT a scientist. A scinetist knows elementary math. You should be ashamed of yourself.

                    How dare you! I may be a beginner in the programming world, but to be spoken to in this way from someone who does not know me is extremely rude! I am indeed a scientist, registered in the UK as a practising biomedical scientist, not that I need to explain myself to you! As for the math, if you took the time to read through the post, you would see that I simply explained myself wrong, and it was the programming that I was struggling with, and not the math. As for being ashamed of myself; I do not know what your problem is, but believe me, I have nothing to be ashamed of, and could now go on to say allot about, and to you, however, I will refrain! Stephen

                    N 1 Reply Last reply
                    0
                    • S stephen darling

                      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

                      F Offline
                      F Offline
                      FunkySteve
                      wrote on last edited by
                      #30

                      Actually, every number is divisible by 5.

                      S 1 Reply Last reply
                      0
                      • F FunkySteve

                        Actually, every number is divisible by 5.

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

                        FunkySteve wrote:

                        Actually, every number is divisible by 5

                        True. Again, my fault for not explaining properly. I meant a modulus of zero, so that 13285 MOD 5 = 0 Got it all sorted now, thanx to most people, disregarding one perticular ignorant comment :confused: Thank you, Steve

                        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

                          F Offline
                          F Offline
                          Fabio Franco
                          wrote on last edited by
                          #32

                          I'd say you're stating the obvious, but then, I remember when I was very young and inexperienced and I couldn't see stuff like this. So, it's not always obvious as one might think. You got my five.

                          "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

                          L 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

                            K Offline
                            K Offline
                            Kenneth Kasajian
                            wrote on last edited by
                            #33

                            Is this discussion really taking place? What is this, 3rd grade?

                            ken@kasajian.com / www.kasajian.com

                            S 1 Reply Last reply
                            0
                            • F Fabio Franco

                              I'd say you're stating the obvious, but then, I remember when I was very young and inexperienced and I couldn't see stuff like this. So, it's not always obvious as one might think. You got my five.

                              "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

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

                              I tend to state facts, even obvious ones, especially when it seems to OP is missing them somehow. Rather than spoon feeding, I prefer to give a gentle push in the right direction... :)

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

                              F 1 Reply Last reply
                              0
                              • K Kenneth Kasajian

                                Is this discussion really taking place? What is this, 3rd grade?

                                ken@kasajian.com / www.kasajian.com

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

                                Kenneth Kasajian wrote:

                                Is this discussion really taking place? What is this, 3rd grade?

                                What is everones problem? I asked how to solve a basic math based problem PROGMATICALLY as I am a beginner when it comes to c#, and I am wishing I never bothered asking! Although, there are a number of people who have offered working solutions. Why must people post such a responce as yours? Did your post really help in anyway? Regards, Stephen

                                K 1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  I tend to state facts, even obvious ones, especially when it seems to OP is missing them somehow. Rather than spoon feeding, I prefer to give a gentle push in the right direction... :)

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                                  F Offline
                                  F Offline
                                  Fabio Franco
                                  wrote on last edited by
                                  #36

                                  Completely agree, I'm totally against the "gimme codezzzz plz" culture.

                                  "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

                                  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.

                                    A Offline
                                    A Offline
                                    ARon_
                                    wrote on last edited by
                                    #37

                                    http://mathworld.wolfram.com/EvenNumber.html[^] Note zero is an even number and by definition dividable by two, the answer is just zero. As a recall from calculus I, you can't divide a number by zero but you can divide a number as the devisor approaches zero.

                                    ARon

                                    1 Reply 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

                                      U Offline
                                      U Offline
                                      User 7955466
                                      wrote on last edited by
                                      #38

                                      Generate a five digit random number, convert to string, replace the right most digit with 5 (or 0), parse the string back to an int. No need to check as anything ending in 5 or 0 is divible by 5.

                                      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

                                        R Offline
                                        R Offline
                                        RDSchaefer
                                        wrote on last edited by
                                        #39

                                        Seriously??? Were you sleeping during elementary math classes? I'm sorry but if I were your boss and you asked me that question I would transfer you out of my department over to HR or maybe Daycare. :omg:

                                        S 1 Reply Last reply
                                        0
                                        • B BobJanova

                                          That is a stupid definition. It would also indicate that -10 is not divisible by 5. There may be abstruse mathematical concepts for which it's useful, I suppose, but for normal maths it is nonsense.

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

                                          x is not a natural number there.

                                          B 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