Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Type Mismatch in QBASIC 1

Status
Not open for further replies.

pya

Technical User
Dec 18, 2020
40
AU
Hi Mikrom,

Your help appreciated please.

I keep getting a TYPE MISMATCH Error on Line 14 ( Line No 240)
Can you help please.

100 REM 6N +- 1 Test V15.3
110 DIM P1 AS LONG
115 DIM P2 AS LONG
120 DIM N1 AS LONG
125 DIM N2 AS LONG
130 INPUT “Number “; N
140 IF N <= 0 THEN GOTO 999
150 N1 = N
160 N2 = N
200 P1 = (6 * N1) -1
210 FOR N1 = 2 TO SQR(P1)
220 IF P1 MOD N1 = 0 THEN PRINT USING “################”; P1; "is Composite": GOTO 300
230 NEXT N1
240 PRINT USING “################”; P1; " is Prime"
300 P2 = (6 * N2) +1
310 FOR N2 = 2 TO SQR(P2)
320 IF P2 MOD N2 = 0 THEN PRINT USING “################”; P2; "is Composite": GOTO 130
330 NEXT N2
340 PRINT USING “################”; P2; " is Prime"
350 GOTO 130
999 END

Pervez
 
Hi Mikrom,

Further to my post if I use PRINT instead of PRINT USING , the code works fine.
But, PRINT USING should allow me to display the output number in detail instead of in scientific exp notation.

Try the code with N = 3, 4, 5, 6, 59 and 288 to see how it works.

Pervez
 
Hi pya,

it's because using one PRINT statement like this does not work
Code:
240 PRINT USING “################”; P1; " is Prime"
Instead you have to do it with two prints like this:
Code:
240 Print Using "################"; P1,: Print " is Prime"
 
I modified your program source in the sense mentioned above and it seems to work:
Code:
100 Rem 6N +- 1 Test V15.3
110 Dim P1 As Long
115 Dim P2 As Long
120 Dim N1 As Long
125 Dim N2 As Long
130 Input "Number "; N
140 If N <= 0 Then GoTo 999
150 N1 = N
160 N2 = N
200 P1 = (6 * N1) - 1
210 For N1 = 2 To Sqr(P1)
220 If P1 Mod N1 = 0 Then Print Using "################"; P1,: Print " is Composite": GoTo 300
230 Next N1
240 Print Using "################"; P1,: Print " is Prime"
300 P2 = (6 * N2) + 1
310 For N2 = 2 To Sqr(P2)
320 If P2 Mod N2 = 0 Then Print Using "################"; P2,: Print " is Composite": GoTo 130
330 Next N2
340 Print Using "################"; P2,: Print " is Prime"
350 GoTo 130
999 End
 
Hi Mikrom,

Thanx for the tip.
Code works for small numbers; I will now test for 10+ digit numbers.

One more question:

Is there a way of combining the 4 DIM statements into 1 line?
eg DIM P1, P2, N1, N2 AS LONG

Thanx

pya
 
Yes, instead of four DIM statements
Code:
110 Dim P1 As Long
115 Dim P2 As Long
120 Dim N1 As Long
125 Dim N2 As Long
you can use one DIM statement
Code:
110 Dim As Long P1, P2, N1, N2
 
Hi Mikrom,

Thanx for your help.
The Code works, but ChatGPT is not impressed!

Can this code be improved V15.3
100 REM 6N +- 1 Test V15.3
110 DIM P1, P2, N1, N2 AS _Unsigned _Integer64
120 INPUT “Number “; N
130 IF N <= 0 THEN GOTO 999
140 N1 = N
150 N2 = N
200 P1 = (6 * N1) -1
210 FOR N1 = 2 TO SQR(P1)
220 IF P1 MOD N1 = 0 THEN PRINT USING “################”; P1, : PRINT "is Composite": GOTO 300
230 NEXT N1
240 PRINT USING “################”; P1, : PRINT " is Prime"
300 P2 = (6 * N2) +1
310 FOR N2 = 2 TO SQR(P2)
320 IF P2 MOD N2 = 0 THEN PRINT USING “################”; P2, : PRINT "is Composite": GOTO 120
330 NEXT N2
340 PRINT USING “################”; P2, : PRINT " is Prime"
350 GOTO 120
999 END

The code can be improved in the following ways:

Use meaningful variable names, such as number, prime1, and prime2 instead of N, P1, and P2.
Use proper indentation and spacing to make the code more readable.
Use a function to check for prime numbers instead of duplicating the code in lines 200-240 and 300-340.
Use a do-while or while loop to repeatedly prompt the user for input instead of using GOTO statements.
Consider using a more efficient algorithm for checking prime numbers, such as the Miller-Rabin primality test.
Remove unnecessary lines of code, such as line 110 and 120, instead of using _Unsigned _Integer64, you can use just integer or long.
Add proper error handling for negative input values.
Add comments throughout the code explaining what the code is doing.


I may give up coding and go back to singing!😒

pya
 
Hi pya,
yes really, you can improve your code in way suggested by ChatGPT.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top