QB64.org Forum

Active Forums => Programs => Topic started by: cobiesmul47 on January 04, 2021, 11:44:48 am

Title: How does one multiply digits of a number?
Post by: cobiesmul47 on January 04, 2021, 11:44:48 am
For example, we have a task to make a program that adds up digits of any number above 1000 - it works.

But there's a second part, to multiply a number below 1000 - how does one make a program for that? If I type in 321 (for example) it just gives me zero, while it should multiply 3, 2, and 1.

Any help is appreciated! currys tv (https://techguychirag.com/what-is-currys-tv/)
Title: Re: How does one multiply digits of a number?
Post by: bplus on January 04, 2021, 11:54:25 am
Welcome to forum @cobiesmul47

Do you want to take a number say 639 and mult digits like 6 * 3 * 9 ? = 162


If so, use same method you used to get the individual digits to add :)
Title: Re: How does one multiply digits of a number?
Post by: SierraKen on January 04, 2021, 05:40:00 pm
I deleted my answer in case this was for school... please show us what you have so far and we can teach you better from there.

Title: Re: How does one multiply digits of a number?
Post by: NOVARSEG on January 04, 2021, 08:02:27 pm
I'm not sure why a number above 1000 would be easier to add up than a number below 1000.

 
Quote
while it should multiply 3, 2, and 1.

like this?

3 * 100  +  2 * 10  + 1  = 321

Are you inputting a string such as "321"  and converting that to a number?

How about

Code: QB64: [Select]
  1. a$ = "1234"
  2.  
  3. FOR x% = 1 TO LEN(a$)
  4.  
  5.     b% = b% + ASC(a$, x%) - 48
  6.  
  7. NEXT x%
  8.  

that -48  pretty neat huh?  but why?
Title: Re: How does one multiply digits of a number?
Post by: bplus on January 04, 2021, 08:21:28 pm
Here is a hint at what might be going wrong.

When you convert a number to a string with STR$(number), if the number is positive the first character found will be a space, ie MID$(STR$(number, 1, 1)) = " ", a space, and that has a VAL() of 0 and 0 times anything = 0.

AND you would NOT run into the same problem adding digits because 0 + anything = anything!
Title: Re: How does one multiply digits of a number?
Post by: FellippeHeitor on January 04, 2021, 08:22:50 pm
Hello, @cobiesmul47.

Although you've already been offered a few solutions to the task you have at hand, I wanna point out that we're all willing to help you debug code you've already written and that didn't work as expected, but we do not encourage our members to write code for anyone. Please bring your own attempt first, so you can get help. Asking for homework to be done is not welcome.

Aside from that, welcome to the forum.

Fellippe.
Title: Re: How does one multiply digits of a number?
Post by: STxAxTIC on January 04, 2021, 10:04:43 pm
Now that we're spoiling homework answers, at least do the assignment properly. Pssshhh... Strings...

Code: QB64: [Select]
  1.  
  2. n0 = 8672
  3.  
  4. n = n0
  5. t = 0
  6.     k = n MOD 10
  7.     n = n - k
  8.     IF (n = 0) AND (k = 0) THEN EXIT DO
  9.     n = n / 10
  10.     t = t + k
  11.  
  12. PRINT n0, t

Note: Initialize t=1 and change + to * to solve the second problem.
Title: Re: How does one multiply digits of a number?
Post by: SierraKen on January 05, 2021, 03:25:15 pm
Bplus, I ran into that problem when I made the solution. The easy way to remove spaces on the left side of strings is to use something like this: A$ = LTRIM$(STR$(A))

I'm guessing this homework is long gone now (or almost long gone), classes usually have 1 or 2 nights to do homework.
Title: Re: How does one multiply digits of a number?
Post by: bplus on January 05, 2021, 03:34:17 pm
Yes, let everyone learn from some homework!

I learned my first instinct to convert to string wasn't that necessary and you can do it all numbers.

Here is my attempt versus STx:
Code: QB64: [Select]
  1. WHILE _KEYDOWN(27) = 0
  2.     n = INT(RND * 1000)
  3.  
  4. ' bplus
  5.     m = n \ 100
  6.     o = (n MOD 100) \ 10
  7.     p = n MOD 10
  8.     PRINT n; ">"; m * o * p
  9.  
  10. 'STx
  11.     n0 = n
  12.     t = 1
  13.     DO
  14.         k = n MOD 10
  15.         n = n - k
  16.         IF (n = 0) AND (k = 0) THEN EXIT DO
  17.         n = n / 10
  18.         t = t * k
  19.     LOOP
  20.  
  21.     PRINT n0, t
  22.  
  23.     SLEEP
  24.  
  25.  
I mop up with LOC but STx is the more general method for any length number.

Do you think teacher would expect to see this from newbies? ;-))
Title: Re: How does one multiply digits of a number?
Post by: STxAxTIC on January 05, 2021, 03:35:20 pm
EDIT: Nevermind, I see your comment under the code.

EDIT EDIT: Haha, yeah, I considered naming my variables "f", "c", "k", "u" to see if the teacher would notice a direct paste and call the kid out.
Title: Re: How does one multiply digits of a number?
Post by: SierraKen on January 05, 2021, 04:02:26 pm
Woops I was wrong, it's multiplying the values on the second one... lol
Title: Re: How does one multiply digits of a number?
Post by: SierraKen on January 05, 2021, 04:09:10 pm
Since everyone has made them already, here is mine. I added comments to most of the lines so he can learn by it.

Code: QB64: [Select]
  1.     CLS
  2.     A = INT(RND * 998) + 1 'Set the first number as A.
  3.     A$ = LTRIM$(STR$(A)) 'Turn A into a string: A$ and trim off the excess blank space that is before the string that it makes.
  4.     A1$ = MID$(A$, 1, 1) 'Set A1$ to the first number in A using MID$. The first 1 in MID$ is the starting point, the second number 1 is the ending point of the string.
  5.     A2$ = MID$(A$, 2, 1) 'Set A2$ the same way you did A1 but start with the 2nd number and then 1 as just the one digit.
  6.     A3$ = MID$(A$, 3, 1) 'Set A3$ the same way you did the last 2, but start as the 3rd number and then 1 as just the one digit.
  7.     A1 = VAL(A1$) 'Set A1 as a numerical variable from the A1$. MID$ only uses strings which is why we had to use strings above.
  8.     A2 = VAL(A2$) 'Set A2 and A3 the same way.
  9.     A3 = VAL(A3$)
  10.     Answer = A1 * A2 * A3 'Here is your answer.
  11.     PRINT A; " > "; Answer 'Now print your answer.
  12.     PRINT: PRINT
  13.     INPUT "Press enter for another one."; b$
  14.  

Title: Re: How does one multiply digits of a number?
Post by: SMcNeill on January 05, 2021, 05:09:57 pm
Since everyone has made them already, here is mine.

Heck, I'll up the ante once more, and do the whole homework problem as it was presented:  "we have a task to make a program that adds up digits of any number above 1000, But there's a second part, to multiply a number below 1000 - how does one make a program for that?"

Code: QB64: [Select]
  1.     INPUT "Enter a number =>"; num
  2.     n = NumberOfDigits(num)
  3.     IF n > 3 THEN
  4.         FOR i = 1 TO n
  5.             result = result + GetDigit(num, i)
  6.         NEXT
  7.         PRINT "The sum of the digits is:";
  8.     ELSE
  9.         result = 1
  10.         FOR i = 1 TO n
  11.             result = result * GetDigit(num, i)
  12.         NEXT
  13.         PRINT "The product of the digits is:";
  14.     END IF
  15.     PRINT result
  16.     PRINT
  17.     PRINT "Do another? (Y/N)"
  18.     DO: _LIMIT 10: i$ = UCASE$(INKEY$): LOOP UNTIL i$ = "Y" OR i$ = "N"
  19.     IF i$ = "N" THEN SYSTEM
  20.     result = 0
  21.  
  22. FUNCTION NumberOfDigits (num AS _INTEGER64)
  23.     DIM tempnum AS _INTEGER64: tempnum = ABS(num)
  24.     WHILE tempnum > 0
  25.         NumberOfDigits = NumberOfDigits + 1
  26.         tempnum = tempnum \ 10
  27.     WEND
  28.  
  29. FUNCTION GetDigit (Num AS _INTEGER64, Digit AS INTEGER)
  30.     GetDigit = (Num MOD 10 ^ Digit) \ 10 ^ (Digit - 1)

I think this little example showcases the difference in style for how I tend to program and how most folks here on the forums do.  When I see this type problem, my brain breaks it down into simple steps:

Step 1: Determine how many digits a number has.
Step 2: How to get any singular digit from that number.
Step 3: Manipulate those digits.

Once my brain has analyzed the problem, I then break it down into tools and sub-sections to solve each task.  I'm notorious to never write "disposable code", which just does one thing and then I can toss it -- instead, I try and write code which can break down into Functions and Subs which I can forever import into use in future programs, without having to rewrite them repeatedly.

I didn't have a function to get NumberOfDigits in a number, but I do now.  I didn't have a function to GetDigit from a number, but I do for it also, now.   

My advice is:  Don't just code to solve today's problems.  Code to create a toolset which can be used to help solve tomorrow's.
Title: Re: How does one multiply digits of a number?
Post by: SMcNeill on January 05, 2021, 05:19:07 pm
Since everyone has made them already, here is mine. I added comments to most of the lines so he can learn by it.

Code: QB64: [Select]
  1.     CLS
  2.     A = INT(RND * 998) + 1 'Set the first number as A.
  3.     A$ = LTRIM$(STR$(A)) 'Turn A into a string: A$ and trim off the excess blank space that is before the string that it makes.
  4.     A1$ = MID$(A$, 1, 1) 'Set A1$ to the first number in A using MID$. The first 1 in MID$ is the starting point, the second number 1 is the ending point of the string.
  5.     A2$ = MID$(A$, 2, 1) 'Set A2$ the same way you did A1 but start with the 2nd number and then 1 as just the one digit.
  6.     A3$ = MID$(A$, 3, 1) 'Set A3$ the same way you did the last 2, but start as the 3rd number and then 1 as just the one digit.
  7.     A1 = VAL(A1$) 'Set A1 as a numerical variable from the A1$. MID$ only uses strings which is why we had to use strings above.
  8.     A2 = VAL(A2$) 'Set A2 and A3 the same way.
  9.     A3 = VAL(A3$)
  10.     Answer = A1 * A2 * A3 'Here is your answer.
  11.     PRINT A; " > "; Answer 'Now print your answer.
  12.     PRINT: PRINT
  13.     INPUT "Press enter for another one."; b$
  14.  

You get a 0 on this project, if I'm your teacher -- it fails.   Try a value of 14...  The answer is NOT 0. :P

See a corrected version below.  ;D

Code: QB64: [Select]
  1.     CLS
  2.     A = INT(RND * 998) + 1 'Set the first number as A.
  3.     A$ = LTRIM$(STR$(A)) 'Turn A into a string: A$ and trim off the excess blank space that is before the string that it makes.
  4.     A1$ = MID$(A$, 1, 1) 'Set A1$ to the first number in A using MID$. The first 1 in MID$ is the starting point, the second number 1 is the ending point of the string.
  5.     A2$ = MID$(A$, 2, 1) 'Set A2$ the same way you did A1 but start with the 2nd number and then 1 as just the one digit.
  6.     A3$ = MID$(A$, 3, 1) 'Set A3$ the same way you did the last 2, but start as the 3rd number and then 1 as just the one digit.
  7.     A1 = VAL(A1$) 'Set A1 as a numerical variable from the A1$. MID$ only uses strings which is why we had to use strings above.
  8.     IF LEN(A2$) THEN A2 = VAL(A2$) ELSE A2 = 1 'Set A2 and A3 the same way.
  9.     IF LEN(A3$) THEN A3 = VAL(A3$) ELSE A3 = 1 ' <=== But don't let them change the result if A2$ or A3$ is blank!!!
  10.     Answer = A1 * A2 * A3 'Here is your answer.
  11.     PRINT A; " > "; Answer 'Now print your answer.
  12.     PRINT: PRINT
  13.     INPUT "Press enter for another one."; b$

Title: Re: How does one multiply digits of a number?
Post by: SierraKen on January 05, 2021, 06:08:58 pm
LOL woops thanks Steve! What happened was that I had a strict 3 digit number and today I tossed in the random code without thinking it could be under 100. LOL

Title: Re: How does one multiply digits of a number?
Post by: SMcNeill on January 05, 2021, 06:22:54 pm
LOL woops thanks Steve! What happened was that I had a strict 3 digit number and today I tossed in the random code without thinking it could be under 100. LOL

Honestly, your answer might be right and mine wrong.  It depends on how the teacher defines the problem.  After all, 14 *is* the same as 014.  Do we count leading zeroes?
Title: Re: How does one multiply digits of a number?
Post by: STxAxTIC on January 05, 2021, 06:25:35 pm
premature optimization is the root of all evil
Title: Re: How does one multiply digits of a number?
Post by: bplus on January 05, 2021, 06:27:12 pm
Well I hope no one feeds Steve's NumberOfDigits Function a negative number.

Signed,  Also Failed. ;-))
Title: Re: How does one multiply digits of a number?
Post by: SMcNeill on January 05, 2021, 06:32:08 pm
Well I hope no one feeds Steve's NumberOfDigits Function a negative number.

Signed,  Also Failed. ;-))

Why would it fail for negative numbers?  Did you miss the compound line?

tempnum = ABS(num)

ABS(-123) = 123...  which would be 3 digits, or are you counting the sign as a digit?
Title: Re: How does one multiply digits of a number?
Post by: bplus on January 05, 2021, 06:33:51 pm
Yes I did miss, I fail again! ;-))
Title: Re: How does one multiply digits of a number?
Post by: SMcNeill on January 05, 2021, 06:35:56 pm
Yes I did miss, I fail again! ;-))

Teach, can I get my grade changed??  LOL!
Title: Re: How does one multiply digits of a number?
Post by: bplus on January 05, 2021, 06:45:09 pm
Maybe teach will show mercy if I do it how I would normally, no need for function creation:
Code: QB64: [Select]
  1.     n = INT(RND * 1000) ' just testing number 0 to 999
  2.     sn$ = STR$(n)
  3.     m = 1
  4.     FOR i = 2 TO LEN(sn$)
  5.         m = m * VAL(MID$(sn$, i, 1))
  6.     NEXT
  7.     PRINT n, m
  8.     SLEEP
  9.