DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   qbasic (http://www.daniweb.com/code/qbasic.html)
-   -   Print out Prime Numbers (http://www.daniweb.com/code/snippet138.html)

vegaseat qbasic syntax
Jan 1st, 2005
I just had to do this to see how it would look like in the code field. This small prime number generator is written in BCX basic, a mildy more modern basic than Qbasic. With BCX basic you can throw in C and assembler code and it would actually compile it. Gives you the familiar comfort of basic, but allows you to venture to C at will.

  1. ' look at a series of numbers and print out the prime numbers
  2. ' prime numbers are divisible only by unity or themselves
  3. ' this is BCX basic, a modern successor to Qbasic
  4.  
  5. DIM A ' defaults to integer
  6.  
  7. FOR A = 1 TO 100
  8. IF IsPrime(A) THEN PRINT A;
  9. NEXT
  10.  
  11. Pause ' make console wait
  12.  
  13. FUNCTION IsPrime(Num)
  14. LOCAL X
  15. ' make exeptions for unity and 2
  16. IF Num = 1 THEN FUNCTION = True
  17. IF Num = 2 THEN FUNCTION = True
  18. ' leave on even numbers
  19. IF MOD(Num, 2) = 0 THEN EXIT FUNCTION
  20. FOR X = 3 TO Num - 1 STEP 2
  21. IF MOD(Num, X) = 0 THEN EXIT FUNCTION
  22. NEXT X
  23. FUNCTION = TRUE ' return true if it's a Prime Number
  24. END FUNCTION