You are here:

Advertisement

WAP to find if given number is divisible by 3 or not

Hi Sachet,

To answer this question, you'll need to remember some early public school math and remainders. We know that if we try and divide with whole numbers, if they are not divisible by each other, there is a left over. For insance, 5 / 2 = 2, remainer 1. The remainder can be found using the MOD operator, which is like division, but will only return the remainder of a division.

DIM my_remainder AS INTEGER my_remainder = 5 MOD 2 PRINT my_remainder REM - OUTPUT: 1

Oppositely, if you want to know if number "X" is divisible by 3, you will know if the remainder is 0, meaning everything divided fully.

I will not give you the answer beyond this; I have now given you the knowledge of how to see if a number is divisible by any number (including 3).

Now you can do what any programmer would do; trial and error. Try some code with numbers that you know. Here is how I would check to see if I had the right answer:

DIM test_number AS INTEGER test_number = 3 '' We know this will work, because 3 / 3 = 1, and 1 is a whole number IF 'PUT YOUR FORMULA HERE USING MOD' = 0 THEN PRINT test_number; " is divisible by 3" ELSE PRINT test_number; " is not diviisble by 3" END IF

Now try changing test_number to other values you know should or should not work. For instance, you should know that 6, 9, 12, 15, 18, 21 should work (formula should equal 0), and that 4, 7, 10, 11, 13 should not work (formula will not equal 0). If your formual gives the correct results for all of these cases, you should be fine.

-Alex

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.

I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future.
Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.**Education/Credentials**

Highschool - 2007