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

    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
                        • U User 7955466

                          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 Offline
                          M Offline
                          Michael A Cochran
                          wrote on last edited by
                          #41

                          This would work as an alternative but the math approach is better (more performant) unless you want the final value to be a string. And, even then the math approach would be faster. (nnnn*10)=nnnn0 It's the same as appending a "0" to a four-digit number but it's much faster.

                          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

                            L Offline
                            L Offline
                            lucien64
                            wrote on last edited by
                            #42

                            I'm sorry but a lot of the answers made me laugh... By definition any number that has been multiplied by 5 is divisible by 5. So the solution is: x=random(low_limit, high_limit)*5; no need to check this! The five digit condition is a little bit more complicated, because it is not clearly defined. is 00005 a five digit number by this definition? Low limit will then be 0, high limit 19999 (the highest 5 digit number fullfilling the condition is 99995). To get a number that is divisable by 5, the ending needs to be 0 or 5. If you need checking, the modulo division is correct.

                            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

                              M Offline
                              M Offline
                              Michael A Cochran
                              wrote on last edited by
                              #43

                              // To generate as random numbers as possible, this variable must only
                              // be initialized once and then reused as much as possible.
                              Random rand = new Random();
                              /// <summary>
                              /// Generates a random 5 digit number that is always divisible by 5.
                              /// </summary>
                              /// <returns>Returns a random integer 5 digits long that is evenly divisible by 5.</returns>
                              private int GetDivBy5()
                              {

                              // rnd.Next(minValue, maxValue) returns a random between minValue
                              // and maxValue, inclusive. Use min/max values of 1000 and 9999 to
                              // yield a random 4 digit number between 1000 and 9999 then multiply
                              // by 10 to get a 5 digit number.  This number will always be
                              // divisible by 5 but will always end in 0.
                              int divBy5 = rand.Next(1000, 9999) \* 10;
                              
                              // To get more random and double the possible result set, randomly
                              // add on 5.  This will yield the total possible set of integers
                              // between 10000 and 99995 that are divisible by 5.
                              if (rand.NextDouble() < 0.5)
                                  return divBy5;
                              else
                                  return divBy5 + 5;
                              

                              }

                              Stephen, you mention you are a coding noobie. So, a short word about random number generators - they're not really very random.
                              All they do is take a seed (beginning) value and run it through a mathmatical algorithm that generates a new number. If you use the same seed value every time, you will get the same "random" number every time. Most random number generators can either self-seed - generally using the current clock value - or you can pass them a seed value when you initialize them. This is why you only want to initialize your random number generator once and reuse it over and over. Once you get past that, the rest is pretty easy. Just use the math several others have already provided. See the comments in the code... <edit> Um, just looked at the thread in the other forum where you defined your requirements a little better. It seems you would like to include 0-9999 in your result set too. You can do this easily enough by changing the min value passed to rand.Next(minValue, maxValue) to 0 instead of 1000. This last bit, is a bit tricky in that in programming terms "00005" is actually not a number but is really a string. To get the leading zeros, you need to convert your final value to a string and use a formatting mask to append the leading zeros. This also assumes "00000" is valid. If it's not, test for it with an "if" and call GetDivBy5 again. So, to get a string with leading ze

                              S J 2 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.

                                R Offline
                                R Offline
                                realJSOP
                                wrote on last edited by
                                #44

                                Knowing math, and knowing how a make a computer perform math are two completely different things. Your statement is wilding inappropriate.

                                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                                -----
                                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                                -----
                                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                                1 Reply Last reply
                                0
                                • R RDSchaefer

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

                                  RDSchaefer wrote:

                                  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.

                                  This is the last time I will even bother with a reply to the outright rude comments! If you were to bother yourself to read all of the posts, you would realise that I already appologised that I had not explained myself properly, and my problem is not with math. I am getting fed up with having to explain this, and to be honest, it is getting hard to hold my tounge with the likes of you! Your not my boss, are you? So why not keep your ignorant comments to yourself.

                                  R 1 Reply Last reply
                                  0
                                  • M Michael A Cochran

                                    // To generate as random numbers as possible, this variable must only
                                    // be initialized once and then reused as much as possible.
                                    Random rand = new Random();
                                    /// <summary>
                                    /// Generates a random 5 digit number that is always divisible by 5.
                                    /// </summary>
                                    /// <returns>Returns a random integer 5 digits long that is evenly divisible by 5.</returns>
                                    private int GetDivBy5()
                                    {

                                    // rnd.Next(minValue, maxValue) returns a random between minValue
                                    // and maxValue, inclusive. Use min/max values of 1000 and 9999 to
                                    // yield a random 4 digit number between 1000 and 9999 then multiply
                                    // by 10 to get a 5 digit number.  This number will always be
                                    // divisible by 5 but will always end in 0.
                                    int divBy5 = rand.Next(1000, 9999) \* 10;
                                    
                                    // To get more random and double the possible result set, randomly
                                    // add on 5.  This will yield the total possible set of integers
                                    // between 10000 and 99995 that are divisible by 5.
                                    if (rand.NextDouble() < 0.5)
                                        return divBy5;
                                    else
                                        return divBy5 + 5;
                                    

                                    }

                                    Stephen, you mention you are a coding noobie. So, a short word about random number generators - they're not really very random.
                                    All they do is take a seed (beginning) value and run it through a mathmatical algorithm that generates a new number. If you use the same seed value every time, you will get the same "random" number every time. Most random number generators can either self-seed - generally using the current clock value - or you can pass them a seed value when you initialize them. This is why you only want to initialize your random number generator once and reuse it over and over. Once you get past that, the rest is pretty easy. Just use the math several others have already provided. See the comments in the code... <edit> Um, just looked at the thread in the other forum where you defined your requirements a little better. It seems you would like to include 0-9999 in your result set too. You can do this easily enough by changing the min value passed to rand.Next(minValue, maxValue) to 0 instead of 1000. This last bit, is a bit tricky in that in programming terms "00005" is actually not a number but is really a string. To get the leading zeros, you need to convert your final value to a string and use a formatting mask to append the leading zeros. This also assumes "00000" is valid. If it's not, test for it with an "if" and call GetDivBy5 again. So, to get a string with leading ze

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

                                    Thank you very much. Sorry I missed this post, and to be honest I am a little p***** off with the negative comments I am receiving, that is why I stopped looking at the responses as it upset me a little that people on here can be so damn rude and insulting, oh, and judgmental, for no apparent reason? Anyway, thank you very much. Kind Regards, Stephen

                                    C M 2 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.

                                      N Offline
                                      N Offline
                                      Not Active
                                      wrote on last edited by
                                      #47

                                      mmwlada wrote:

                                      You should be ashamed of yourself.

                                      He has nothing to be ashamed of. You do.


                                      I know the language. I've read a book. - _Madmatt "The OP herself was not sure about her question" "The OP is from India and I know what she meant."

                                      1 Reply Last reply
                                      0
                                      • S stephen darling

                                        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 Offline
                                        N Offline
                                        Not Active
                                        wrote on last edited by
                                        #48

                                        stephen.darling wrote:

                                        I have nothing to be ashamed of

                                        Absolutely correct.


                                        I know the language. I've read a book. - _Madmatt "The OP herself was not sure about her question" "The OP is from India and I know what she meant."

                                        1 Reply Last reply
                                        0
                                        • S stephen darling

                                          RDSchaefer wrote:

                                          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.

                                          This is the last time I will even bother with a reply to the outright rude comments! If you were to bother yourself to read all of the posts, you would realise that I already appologised that I had not explained myself properly, and my problem is not with math. I am getting fed up with having to explain this, and to be honest, it is getting hard to hold my tounge with the likes of you! Your not my boss, are you? So why not keep your ignorant comments to yourself.

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

                                          I was not commenting on your coding skills and I did read all the posts, however I do apologize. I was just so astounded to think someone could actually not know that (Any number ending in 5 or 0 is divisible by 5), that I reacted in a knee-jerk fashion. I also didn't solve your problem because it had already been well answered. BTW, you really need to develop thicker skin. There will always be some people who know so much more than you that they sometimes get irritated answering the same, in their opinion, dumb questions over and over again. It still happens to me occasionally.

                                          S 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