Click here to Skip to main content
15,891,033 members
Home / Discussions / C#
   

C#

 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
David198722-Aug-11 20:49
David198722-Aug-11 20:49 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
BobJanova22-Aug-11 23:48
BobJanova22-Aug-11 23:48 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
David198723-Aug-11 4:14
David198723-Aug-11 4:14 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
BobJanova23-Aug-11 11:02
BobJanova23-Aug-11 11:02 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
ARon_23-Aug-11 4:06
ARon_23-Aug-11 4:06 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
KP Lee23-Aug-11 8:38
KP Lee23-Aug-11 8:38 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
David198723-Aug-11 8:49
David198723-Aug-11 8:49 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
KP Lee23-Aug-11 11:28
KP Lee23-Aug-11 11:28 
I KNEW there was a reason why I liked num -= num%5; better! Smile | :)

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)
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
KP Lee23-Aug-11 11:31
KP Lee23-Aug-11 11:31 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
riced22-Aug-11 4:22
riced22-Aug-11 4:22 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
stephen.darling22-Aug-11 4:59
stephen.darling22-Aug-11 4:59 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
Mycroft Holmes22-Aug-11 14:09
professionalMycroft Holmes22-Aug-11 14:09 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
mmwlada23-Aug-11 2:32
professionalmmwlada23-Aug-11 2:32 
GeneralRe: How do I generate a number divisable by 5, and check it? PinPopular
stephen.darling23-Aug-11 2:40
stephen.darling23-Aug-11 2:40 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
Not Active23-Aug-11 5:43
mentorNot Active23-Aug-11 5:43 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
#realJSOP23-Aug-11 5:19
mve#realJSOP23-Aug-11 5:19 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
Not Active23-Aug-11 5:39
mentorNot Active23-Aug-11 5:39 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
FunkySteve23-Aug-11 2:47
FunkySteve23-Aug-11 2:47 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
stephen.darling23-Aug-11 3:00
stephen.darling23-Aug-11 3:00 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
KP Lee23-Aug-11 8:43
KP Lee23-Aug-11 8:43 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
riced23-Aug-11 8:56
riced23-Aug-11 8:56 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
KP Lee23-Aug-11 9:56
KP Lee23-Aug-11 9:56 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
riced24-Aug-11 6:56
riced24-Aug-11 6:56 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
KP Lee24-Aug-11 9:58
KP Lee24-Aug-11 9:58 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
riced24-Aug-11 10:21
riced24-Aug-11 10:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.