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.
  • 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
        • S stephen darling

          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 Offline
          C Offline
          Chris Maunder
          wrote on last edited by
          #50

          Everyone's a Legend in Their Own Lunchtime. You've learned a lesson - whether you wished to or not - about being precise in framing your question, so I hope that at least that, and the sensible answers you did get, helped. As to the others who felt it was easier to ridicule than to stop for a second and realise there was more to it, I apologise.

          cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

          S 1 Reply Last reply
          0
          • S stephen darling

            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

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

            You're welcome, Stephen. Please keep in mind that mad geniuses as many programmers may be, few people tend to lump us programmers into the "high social skills" cliche. It's been a while since I worked with scientists but I seem to recall from my university days they can be somewhat socially lacking too. I remember this one physicist... Ahem... :omg: Uh, anyway, its probably best to just take the "water off a ducks back" approach - no one really means to insult - especially if you want any of us social deviants to actually help you. :laugh:

            S 1 Reply Last reply
            0
            • R RDSchaefer

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

              That’s OK, I was already upset by someone else's comment, which is why I reacted the way I did.

              RDSchaefer wrote:

              BTW, you really need to develop thicker skin.

              Actually, I have a thick skin. Growing up with the surname "Darling" ensures that you develop one quickly :) I am a little tired today, and when I received a comment from someone on here telling me that I was "NOT a scientist" when they don’t know me, and have never spoke to me, was what made me flip a little. I can understand that it happens, and normally it would be water off a ducks back, however, on this occasion it caused me to respond. Kind Regards, Stephen

              1 Reply Last reply
              0
              • C Chris Maunder

                Everyone's a Legend in Their Own Lunchtime. You've learned a lesson - whether you wished to or not - about being precise in framing your question, so I hope that at least that, and the sensible answers you did get, helped. As to the others who felt it was easier to ridicule than to stop for a second and realise there was more to it, I apologise.

                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

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

                Chris Maunder wrote:

                Everyone's a Legend in Their Own Lunchtime.

                Agreed!

                Chris Maunder wrote:

                You've learned a lesson - whether you wished to or not

                Actually I learned two. Yes; one was indeed to think allot more before I pose my question!

                Chris Maunder wrote:

                As to the others who felt it was easier to ridicule than to stop for a second and realise there was more to it, I apologise.

                You have nothing to apologise for, and it was really just the one statement that I was not a scientist that caused me to react. Never mind, I have moved on :-D Kind Regards, Stephen

                1 Reply Last reply
                0
                • M Michael A Cochran

                  You're welcome, Stephen. Please keep in mind that mad geniuses as many programmers may be, few people tend to lump us programmers into the "high social skills" cliche. It's been a while since I worked with scientists but I seem to recall from my university days they can be somewhat socially lacking too. I remember this one physicist... Ahem... :omg: Uh, anyway, its probably best to just take the "water off a ducks back" approach - no one really means to insult - especially if you want any of us social deviants to actually help you. :laugh:

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

                  Michael A. Cochran wrote:

                  It's been a while since I worked with scientists but I seem to recall from my university days they can be somewhat socially lacking too.

                  Could not agree more! At the end of the day we are all human.

                  Michael A. Cochran wrote:

                  it’s probably best to just take the "water off a ducks back" approach

                  I normally do, however, being told by someone that I had never met or spoke to that I was not a scientist was what got to me. It was not just an insult and ignorant statement, but it was not even to do with programming and in my own opinion was a personal attack that was not called for. Anyway, I have taken what I need from this post, got my code to work and moved on :) Kind Regards, Stephen

                  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

                    V Offline
                    V Offline
                    Vite Falcon
                    wrote on last edited by
                    #55

                    x = generate random number between 10000 and 99995 remainder to be added = 5 - x MOD 5 Therefore, the resultant random number should be x + 5 - x MOD 5 Here's a sample code: http://ideone.com/LxgrC[^]

                    a.k.a. Vite Phoenix and Vite Zeus... Proud member and co-founder of OlympianZ

                    M F 2 Replies Last reply
                    0
                    • D David1987

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

                      K Offline
                      K Offline
                      KP Lee
                      wrote on last edited by
                      #56

                      Actually 0 is divisable by any number. :laugh:

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

                        K Offline
                        K Offline
                        KP Lee
                        wrote on last edited by
                        #57

                        You can't just add 5. Say your result is 135978653, adding 5, the last digit is 8, not divisable by 5

                        R 1 Reply Last reply
                        0
                        • K KP Lee

                          Actually 0 is divisable by any number. :laugh:

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

                          Good luck dividing zero by zero.

                          K 2 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

                            K Offline
                            K Offline
                            KP Lee
                            wrote on last edited by
                            #59

                            You are getting all sorts of advice. Multiplying by 10 can result in overflow. The easiest is: num -= num%5; IE subtract the remainder if you divided by five from the result. Another way: num = (num/5) * 5; That will NEVER cause an overflow in most languages. If you are using vb.net, besides the syntax replace "/" with "\".

                            1 Reply Last reply
                            0
                            • K KP Lee

                              You can't just add 5. Say your result is 135978653, adding 5, the last digit is 8, not divisable by 5

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

                              I did say multiply by 10 then add 5. I also said you could add 5 or 10 - but adding 10 is silly. The multiplication automatically makes it divisible by 5. :laugh:

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

                              K 1 Reply Last reply
                              0
                              • V Vite Falcon

                                x = generate random number between 10000 and 99995 remainder to be added = 5 - x MOD 5 Therefore, the resultant random number should be x + 5 - x MOD 5 Here's a sample code: http://ideone.com/LxgrC[^]

                                a.k.a. Vite Phoenix and Vite Zeus... Proud member and co-founder of OlympianZ

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

                                Why didn't I think of that? :rolleyes: Too much business programming, I guess. We don't use modulus in business programming much - at least I haven't had the need. Correct, of course. Much more elegant. If Stephen is still listening, in C# it becomes;

                                // 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 number between 0 and 99995 that is always divisible by 5.
                                /// </summary>
                                /// <returns>Returns a random integer up to 5 digits long that is evenly divisible by 5.</returns>
                                private int GetDivBy5()
                                {
                                // Get a random number between 0 and 99999.
                                int divBy5 = rand.Next(0, 99999);
                                // Subtract the remainder of (n/5) to make n divisible by 5.
                                return divBy5 - (divBy5 % 5);
                                }

                                private void button1_Click(object sender, EventArgs e)
                                {
                                this.textBox1.Text = this.GetDivBy5().ToString("00000");

                                }

                                1 Reply Last reply
                                0
                                • R riced

                                  I did say multiply by 10 then add 5. I also said you could add 5 or 10 - but adding 10 is silly. The multiplication automatically makes it divisible by 5. :laugh:

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

                                  K Offline
                                  K Offline
                                  KP Lee
                                  wrote on last edited by
                                  #62

                                  Yes, I did read that, I was tweaking. Seriously, multiplying exposes an overflow possiblity. And why 10? num = (num/5)*5; does the trick num -= num%5; also assures overflow won't occur even if you are within 5 of overflow in either positive or negative direction.

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

                                    M Offline
                                    M Offline
                                    mooman27
                                    wrote on last edited by
                                    #63

                                    This solution is not correct - it exhibits a possible overflow error into 6 digits. Consider: 4 Random digits all come out as 9 (multiply by 10 = 99990), then add 10 you get 100000 which does not meet the requirements... The correct solution is: Generate a four digit random number Multiply it by 10 (to shift into correct position) Then randomly choose to make the last digit a 5 or a 0 (adding 10 is the incorrect step) The result is divisible by 5. Regards David.

                                    R 1 Reply Last reply
                                    0
                                    • D David1987

                                      x is not a natural number there.

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

                                      And your point is? (Neither is 0, right?) Both are representable in the number system, and both divide by 5 in any reasonable definition.

                                      1 Reply Last reply
                                      0
                                      • D David1987

                                        Good luck dividing zero by zero.

                                        K Offline
                                        K Offline
                                        KP Lee
                                        wrote on last edited by
                                        #65

                                        I KNEW there was a reason why I liked num -= num%5; better! :) How often have you seen a sizable random number generator produce 0 as an answer? (OK, you can easily set your random number limit so *10 will never overflow.) The original request is divisable by 5 and only affecting the last digit, not the whole number. Lets see: convert the int to a string, take a substring 1 less than the length, add "5" to the string and then convert back to int. There's quite a few ways to get this to work, multiplying by 10 and adding 5 will ONLY work correctly when the random number IS zero. Exactly where did I say to divide by zero? If num is zero, num=(num/5)*5 will produce zero. The highest numbers in int16, int, and int64 all end in decimal 7 and this process will work in all languages, the lowest number ends in 8. That will work in reasonable languages, VB.NET will blow up with an overflow. (In that language use num=(num\5)*5) (Actually VB is a reasonable language, you just have to know the gotchas like 5/3 is 2, not 1. I spent a little time swearing at its math until I found "\". I haven't found a use for round-up but if I did, VB would actually work quite a bit better than most languages for that.) Also good luck executing 5/0! (Or ANY other number by 0)

                                        1 Reply Last reply
                                        0
                                        • D David1987

                                          Good luck dividing zero by zero.

                                          K Offline
                                          K Offline
                                          KP Lee
                                          wrote on last edited by
                                          #66

                                          Sorry, different thread. I did say any number and you are totally right and the rest of my rant was uncalled for.

                                          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