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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Chopping a Number 1

Status
Not open for further replies.

pya

Technical User
Dec 18, 2020
40
0
0
AU
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.

Your help appreciated.

Thanx

pya
 
Hi,

Use the MID$() function
Code:
LET A = 123456

PRINT MID$(A,1,2), MID$(A,3,2), MID$(A,5,2),

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"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!
 
...or
Code:
LET A = 123456
FOR X = 0 TO 2
PRINT MID$(A,X*2+1,2)
NEXT

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"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!
 
Skip,

Thanx, mate.

Much appreciated.

pya
 
Hi,

MID$ works with strings, not numbers, therefore the error

With numbers use operators:
[ul]
[li]\ : result of integer division[/li]
[li]mod : modulo or remainder of division[/li]
[/ul]

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
 
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
 
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
 
Hi mikrom,

Thanx for your help.

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

 
 https://files.engineering.com/getfile.aspx?folder=ebee573d-9840-4522-8609-13d21be392a5&file=Program_to_Guess_Secret_Number.docx
In my QB64 your program does not work for input number 99

pya said:
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
but instead only
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


 
Hi mikrom,

Thanx for your quick response.

Your explanations are very clear.
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
 
pya said:
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 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
 
Pya, have you figured out why your program does not work for input number 99 ?
 
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).

Any help you can provide will be appreciated.

Regards

pya
 
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
 
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
 
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.
 
Hi pya,
You asked for numbers greater than 16 digits.

I found this description of QB64 datatypes:
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 [URL unfurl="true"]https://www.qb64.org/wiki/Data_types#Unsigned_Integer_types[/URL]

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
 
Thanx mikrom.

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

Thanx for all your help.

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

Regards

pya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top