×
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.

# Chopping a Number

## Chopping a Number

(OP)
Folks,

How do I chop and print a 6 digit number?

If my number is 123456, I want to out put 12 or 34 or 56.

Thanx

pya

### RE: Chopping a Number

Hi,

Use the MID$() function #### CODE LET A = 123456 PRINT MID$(A,1,2), MID$(A,3,2), MID$(A,5,2), 

Skip,

Just traded in my OLD subtlety...
for a NUance!

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!

### RE: Chopping a Number

...or

#### CODE

LET A = 123456
FOR X = 0 TO 2
PRINT MID$(A,X*2+1,2) NEXT  Skip, Just traded in my OLD subtlety... for a NUance! "The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein You Matter... unless you multiply yourself by the speed of light squared, then... You Energy! ### RE: Chopping a Number (OP) Skip, Thanx, mate. Much appreciated. pya ### RE: Chopping a Number Hi, MID$ works with strings, not numbers, therefore the error

With numbers use operators:
• \ : result of integer division
• mod : modulo or remainder of division
Example - tested with BASIC256:

#### CODE

rem If number is 123456, I want to out put 12 or 34 or 56
N = 123456

A = N \ 10000
T = N \ 100
B = T mod 100
C = N mod 100

print "N = ", N
print "A = ", A
print "B = ", B
print "C = ", C 

Output:

#### CODE

N =           123456
A =           12
B =           34
C =           56 

### RE: Chopping a Number

or you can do that in the loop too:

#### CODE

rem Tested with BASIC256
N = 123456
print N

while N > 0
X = N mod 100
print X
N = N \ 100
end while 

### RE: Chopping a Number

These loops work with QB64

#### CODE

Rem Using loops
NN = 123456
Print "number =", NN

N = NN
Print "do-while-loop"
Do While N > 0
X = N Mod 100
N = N \ 100
Print X
Loop

N = NN
Print "while-wend loop"
While N > 0
X = N Mod 100
N = N \ 100
Print X
Wend

N = NN
Print "loop with exit"
Do
If N = 0 Then Exit Do
X = N Mod 100
N = N \ 100
Print X
Loop 

### RE: Chopping a Number

(OP)
Hi mikrom,

I used your previous post and wrote a simple number guessing program for my grandkids.

I also picked up info from Google but I do not understand Lines 80, 100 and 110. (see attached File).

If you can clarify I would be grateful.

Also, see if you can 'break' the program.
I would love to make it foolproof before I show it to my kids!

Thanx

pya

### RE: Chopping a Number

In my QB64 your program does not work for input number 99

#### Quote (pya)

but I do not understand Lines 80, 100 and 110. (see attached File)

this reads one character from keyboard, i.e. it waits until you press a key on keyboard:

#### CODE

80 K$= Input$(1)

this is integer division, that mean division without rest:

#### CODE

100 T = N \ 100
if you have for example N = 5050505 then T = N \ 100 = 50505

this is modulo operation, that means the rest after the integer division:

#### CODE

110 B = T Mod 100
if you have for example T = 50505 then B = T Mod 100 = 5

In your case you don't need 2 lines

#### CODE

100 T = N \ 100
110 B = T Mod 100 

#### CODE

100 B = N Mod 100
or if you don't like Mod operator you can use instead

#### CODE

100 T = N \ 100
110 B = N - 100*T 

### RE: Chopping a Number

(OP)
Hi mikrom,

And, thanx to you I am increasing my knowledge.
I did Fortran 4 programming in college but that was in the 70s.

Once again, thanx for all your help.

I have 1 more question if you don't mind.

I use a PRINT statement to insert a blank line.
If I want to insert multiple blank lines, say 3, how do I do that?

Regards & Thanx

pya

### RE: Chopping a Number

#### Quote (pya)

I use a PRINT statement to insert a blank line.
If I want to insert multiple blank lines, say 3, how do I do that?
you can use 3 prints

#### CODE

Print
Print
Print 
or you can use Line Feed character which is hexadecimal 0A, that is decimal 10 (see https://developer.mozilla.org/en-US/docs/Glossary/...)
then you can do following

#### CODE

Rem 3 blank lines with LF
LF$= Chr$(10)
Print LF$+ LF$ 
i.e. you print 2 LineFeed characters and the third will be inserted by PRINT statement

### RE: Chopping a Number

Pya, have you figured out why your program does not work for input number 99 ?

### RE: Chopping a Number

(OP)
Hi mikrom,

Once again thanx for your help.

No, I have not worked out why it is outputting a 0 when the Input is 99.

In fact, I am trying to increase the range of the input from 1 to 999 (or 998).

Regards

pya

### RE: Chopping a Number

Hi pya,

You are using magic number 13837 * 73 = 1010101, but when you multiply it with 99 then the result is too big for QB64 and this causes inaccuracy in further calculations.

For example, when you try this

#### CODE

MagicNumber = 13837 * 73
Print "MagicNumber =", MagicNumber

NUMBER = 99
Print "NUMBER =", NUMBER

N = MagicNumber * NUMBER
Print "MmagicNumber * NUMBER =", N

T = N \ 100
Print "T =", T
B = N - 100 * T
Print "B =", B 
then the result in QB64 is incorrect:

#### CODE

MagicNumber = 1010101
NUMBER =      99
MmagicNumber * NUMBER =     1E+08
T =           1000000
B =           0 

Btw, it is problem in QB64 only, in Basic256 your program works.

To work around this problem in QB64, you have to take smaller magic number.
So if you take instead of
1010101 = 13837 * 73
smaller number
10101 = 3 * 7 * 13 * 37
than it will work in QB64

Now, when you try this

#### CODE

MagicNumber = 3 * 7 * 13 * 37
Print "MagicNumber =", MagicNumber

NUMBER = 99
Print "NUMBER =", NUMBER

N = MagicNumber * NUMBER
Print "MmagicNumber * NUMBER =", N

T = N \ 100
Print "T =", T
B = N - 100 * T
Print "B =", B 
then the result in QB64 is correct:

#### CODE

MagicNumber = 10101
NUMBER =      99
MmagicNumber * NUMBER =     999999
T =           9999
B =           99 

So your corrected program which works in QB64 for input number 99 could be:

#### CODE

10 Cls
20 Print
30 Print "Enter any Number between 1 and 99"
40 Input "Enter your Secret Number"; NUMBER
50 If NUMBER <> Int(NUMBER) GoTo 30
60 If NUMBER < 1 Or NUMBER > 99 GoTo 30
70 Cls
80 K$= Input$(1)
90 N = 3 * 7 * 13 * 37 * NUMBER
100 B = N Mod 100
110 Color 14, 0, 0
120 Print "Your Secret Number is   = ", B
130 Print
140 Color 7, 0, 0
150 GoTo 30
999 End 

(OP)
Brilliant!

### RE: Chopping a Number

(OP)
hi mikrom,

Is BASIC256 more advanced than QB64?

Is it possible to get and install Basic256?

Will Basic256 handle large numbers? Numbers greater than 16 digits?

and, if yes, will my QB64 programs work in Basic256?

Regards

pya

### RE: Chopping a Number

Hi pya,

Regarding what you are programming, I don't know if Basic256 is more advanced than QB64 - in my opinion, not too much. For example your example works in Basic256 with

#### CODE

MagicNumber = 1010101010101
but it will fail with a bigger number e.g.

#### CODE

MagicNumber = 101010101010101

Your QB64 programs will probably not work in Basic256, because for example as I mentioned above Basic256 only support one type of while loop and QB64 supports many types.
Basic256 seems to be a new language dialect, while QB64 is compatible with several classic Basic dialects such as QBasic and others.
But you can install it alongside QB64, compare both and if you don't like it, then you can uninstall it.

### RE: Chopping a Number

Hi pya,
You asked for numbers greater than 16 digits.

I found this description of QB64 datatypes:
https://www.qb64.org/wiki/Data_types#Unsigned_Inte...
By default when data type is not declared the variables are handled as SINGLE datatype, so if we want to work with bigger numbers we can declare them as DOUBLE.
When we only need very big positive integers, we can use _UNSIGNED _INTEGER64 datatype.
This datatype is 8 bytes = 64 bits long, so we can work with numbers from 0 to 2^64 - 1 = 18 446 744 073 709 551 615, which is more than 16 digits.

I tried in QB64 this example and it looks good:

#### CODE

Rem data types - see:
Rem https://www.qb64.org/wiki/Data_types#Unsigned_Integer_types

Print "*using DOUBLE variables:"
Dim As Double MagicNumberD, NumberD, ND, TD, BD

MagicNumberD = 1010101010101

Print "MagicNumber =", MagicNumberD

NumberD = 99
Print "NUMBER =", NumberD

ND = MagicNumberD * NumberD
Print "MmagicNumber * NUMBER =", ND

TD = ND \ 100
Print "T =", TD
BD = ND - 100 * TD
Print "B =", BD
Print

Rem ******************************************
Print "*using _UNSIGNED _INTEGER64 variables:"

Dim As _Unsigned _Integer64 MagicNumberU, NumberU, NU, TU, BU

MagicNumberU = 10101010101010101

Print "MagicNumber =", MagicNumberU

NumberU = 99
Print "NUMBER =", NumberU

NU = MagicNumberU * NumberU
Print "MmagicNumber * NUMBER =", NU

TU = NU \ 100
Print "T =", TU
BU = NU - 100 * TU
Print "B =", BU
Print

MagicNumberU = 1001001001001001

Print "MagicNumber =", MagicNumberU

NumberU = 999
Print "NUMBER =", NumberU

NU = MagicNumberU * NumberU
Print "MmagicNumber * NUMBER =", NU

TU = NU \ 1000
Print "T =", TU
BU = NU - 1000 * TU
Print "B =", BU
Print 

### RE: Chopping a Number

(OP)
Thanx mikrom.

I will now spend a fair amount of time trying to understand and run your code.

I will now try and leave you alone - till next time!

Regards

pya

### RE: Chopping a Number

(OP)
Hi mikrom,

I December 2020 I asked about how to handle large numbers in QB64 as I was stuck at 16 digits.
You advised that I use strings.

But today I used

DIM As _Unsigned _Integer64, P

and correctly tested a 20-digit Prime!!

THANX

pya

### RE: Chopping a Number

Hi pya,

Oh yeah, back then in 2020 I wasn't very interested in Basic and back then I didn't find and read this important article about QB64 data types either
https://www.qb64.org/wiki/Data_types.
I just thought that Basic is an old programming language and that the QB64 compiler certainly doesn't have the ability to work with large numbers.
I only found this article yesterday and it completely changed my mind. But something like that also happens
So I'm already smarter today and I know that QB64 has various built-in data types, including those that allow you to work with large numbers.

By the way, Basic was my first programming language that I learned at college in the 1980s.

### RE: Chopping a Number

I finally installed QB64 on my Linux PC and was able to compare it with the BASIC256 that I had previously installed.

In terms of speed when processing numbers - for example when searching for prime numbers - the QB64 is far superior to the BASIC256.

But both are really good, and what's great is that they are both cross-platform for Linux and Windows.

For someone who is just starting out programming, BASIC256 is definitely easier to use - very nice and easy to use.

Those who, on the other hand, have a little more programming experience, are not afraid of the very classic retro look of the IDE and want to develop astonishingly powerful multi-platform applications in Basic, will appreciate the QB64 more.

### RE: Chopping a Number

(OP)
Thanx for the extra info mikrom.

I am quite happy with my QB64.

Although I still run into problems with large numbers when testing for primes.

DIM As _Unsigned _Integer64, P
correctly tested a 20-digit Prime BUT fails when testing 21 digit numbers.
The screen input will not allow input of the 21st digit during input.
Very strange.

If and only if, when you have time, can you help.

I do coding for fun and to keep my grey cells ticking. (along with Sudoku).
So, if you are busy and you feel I am stretching our online friendship toooo far, please forget and forgive!

I can send you my Prime Testing program if you wish.

Regards

pya

### RE: Chopping a Number

Hi pya,

In the table named Data type limits here https://www.qb64.org/wiki/Data_types#Unsigned_Inte...
you can see that the Maximum value of UNSIGNED _INTEGER64 is 18,446,744,073,709,551,615
That's the 20 digit limit. It seems, that you reached the limit, 21 digit integers are not possible with this data type.

### RE: Chopping a Number

(OP)
Any other data type available for numbers larger than 20 digits?

### RE: Chopping a Number

The data type _FLOAT supports larger numbers, but it is unfortunately not suitable for calculations with large integers. For example, when I tried to find all divisors of the number N = 12345678901234567890, then using UNSIGNED _INTEGER64 data type delivered me first three divisors 2, 3, 5 which is true. But when I changed the data type to _FLOAT, then the program delivered me first three divisors 2, 4, 7, which is false. It did not calculate the divisor 3, but 3 is sure the divisor of N because sum of digits of N is 90 what is divisible by 3. Similarly it did not calculate the divisor 5, which must be true because N ends with 0.

### RE: Chopping a Number

(OP)
I learnt something new today.

Thanx

pya

#### 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!