×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Data Type Mismatch in QBASIC

## Data Type Mismatch in QBASIC

(OP)
Hi Mikrom,

I keep getting a TYPE MISMATCH Error on Line 14 ( Line No 240)

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

### RE: Data Type Mismatch in QBASIC

(OP)
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

### RE: Data Type Mismatch in QBASIC

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"

### RE: Data Type Mismatch in QBASIC

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 

### RE: Data Type Mismatch in QBASIC

(OP)
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

### RE: Data Type Mismatch in QBASIC

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

### RE: Data Type Mismatch in QBASIC

(OP)
Hi Mikrom,

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.

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

pya

### RE: Data Type Mismatch in QBASIC

Hi pya,
yes really, you can improve your code in way suggested by ChatGPT.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!