Print out Prime Numbers

vegaseat 0 Tallied Votes 106 Views Share

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.

' look at a series of numbers and print out the prime numbers
' prime numbers are divisible only by unity or themselves
' this is BCX basic, a modern successor to Qbasic

Dim A    ' defaults to integer

For A = 1 To 100
  If IsPrime(A) Then Print A;
Next

Pause   ' make console wait

Function IsPrime(Num)
  Local X
  ' make exeptions for unity and 2
  If Num = 1 Then Function = True
  If Num = 2 Then Function = True
  ' leave on even numbers
  If Mod(Num, 2) = 0 Then Exit Function
  For X = 3 To Num - 1 Step 2
    If Mod(Num, X) = 0 Then Exit Function
  Next X
  Function = TRUE  ' return true if it's a Prime Number
End Function
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Well, indent doesn't work and highlighting is hit and miss, just like the real Qbasic code snippet.

Dani 4,346 The Queen of DaniWeb Administrator Featured Poster Premium Member

Working on it :)

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Thanks, I knew you would!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Prime numbers are:
Any natural number greater than 1 that has the two divisors 1 and itself.

Defined in: http://en.wikipedia.org/wiki/Prime_number

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.