Author Topic: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35  (Read 1791 times)

0 Members and 1 Guest are viewing this topic.

Offline Asil

  • Newbie
  • Posts: 3
i have 2 integer variable assigned 3 and 5 and i wanna make 1 integer variable and assign it to 35 by combining to integer value.. help pls. thnks..

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Num1 = 23
Num2 = 45
Num3 = VAL(_TRIM$(STR$(Num1)) + _TRIM$(STR$(Num2)))
PRINT Num3
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
Num1 = 3
Num2 = 5
Num3 = Num1 * 10 + Num2
Print Num3
In order to understand recursion, one must first understand recursion.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Num1 = 3
Num2 = 5
Num3 = Num1 * 10 + Num2
Print Num3

The only thing with this answer is that Asii mentioned adding integer to integer.  If Num2 is greater than 10, you wouldn’t get a proper result; which is why I turned them into strings and added them.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
Yep, I see what you mean. I provided an example of how to use power of 10 to achieve an answer for the specific scenario he gave.

With my example his numbers would always need to be 0 through 9.

Num1 = 2
Num2 = 5
Num3 = 8
Combined = Num1 * 100 + Num2 * 10 + Num3
PRINT Combined
In order to understand recursion, one must first understand recursion.

Offline Asil

  • Newbie
  • Posts: 3
i did in this way it works atm. i got 2 string together and convert to int with Val function...
NumberCombined = VAL(MID$(t$, 1, 1) + MID$(t$, 2, 1))

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Hi.

This is my solution for INTEGERS. At first I was sad that QB64 does not have a function that returns the length of a number (number of digits) without conversion to a string, but then I sat down a little in the chair and realized that it could simply be written as a custom function. This, as it is, works for integers, if the function were to return the length, including the places to the right of the decimal point, it would have to be modified. About how do it for decimals, I'm now thinking. But, this is really interest thing.
Code: QB64: [Select]
  1. num1 = 157
  2. num2 = 311
  3. 'result 157311
  4.  
  5. l = LENGHT(num2) 'this is NUMERIC alternative (fastest) for slow STRING operations >>> 'l = LEN(STR$(num2)) - 1
  6. PRINT num1 * 10 ^ l + num2
  7.  
  8. FUNCTION LENGHT (number)
  9.     IF number = 0 THEN LENGHT = 1: EXIT FUNCTION
  10.     IF number <> number \ 1 THEN PRINT "Number is not integer": EXIT FUNCTION
  11.     L = 1
  12.     DO UNTIL number / L < 1
  13.         L = L * 10
  14.         LENGHT = LENGHT + 1
  15.     LOOP
  16.  

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
I like to point out the obvious.

Code: QB64: [Select]
  1.   INPUT "Gimme a number ", n#
  2.   IF n# = 0 THEN EXIT DO
  3.   log10 = LOG(n#) / LOG(10#) + 1
  4.   PRINT INT(log10); " digits"
  5.  
It works better if you plug it in.