Author Topic: How does one multiply digits of a number?  (Read 5862 times)

0 Members and 1 Guest are viewing this topic.

Offline cobiesmul47

  • Newbie
  • Posts: 1
    • View Profile
How does one multiply digits of a number?
« 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
« Last Edit: January 06, 2021, 06:18:23 pm by cobiesmul47 »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How does one multiply digits of a number?
« Reply #1 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 :)
« Last Edit: January 04, 2021, 11:55:46 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: How does one multiply digits of a number?
« Reply #2 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.

« Last Edit: January 04, 2021, 05:50:40 pm by SierraKen »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: How does one multiply digits of a number?
« Reply #3 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?
« Last Edit: January 04, 2021, 08:39:16 pm by NOVARSEG »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How does one multiply digits of a number?
« Reply #4 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!
« Last Edit: January 04, 2021, 08:23:06 pm by bplus »

FellippeHeitor

  • Guest
Re: How does one multiply digits of a number?
« Reply #5 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.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: How does one multiply digits of a number?
« Reply #6 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.
« Last Edit: January 04, 2021, 10:15:52 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: How does one multiply digits of a number?
« Reply #7 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.
« Last Edit: January 05, 2021, 03:26:55 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How does one multiply digits of a number?
« Reply #8 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? ;-))
« Last Edit: January 05, 2021, 03:37:27 pm by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: How does one multiply digits of a number?
« Reply #9 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.
« Last Edit: January 05, 2021, 03:38:44 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: How does one multiply digits of a number?
« Reply #10 on: January 05, 2021, 04:02:26 pm »
Woops I was wrong, it's multiplying the values on the second one... lol
« Last Edit: January 05, 2021, 04:03:41 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: How does one multiply digits of a number?
« Reply #11 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.  

« Last Edit: January 05, 2021, 04:13:49 pm by SierraKen »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How does one multiply digits of a number?
« Reply #12 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.
« Last Edit: January 05, 2021, 06:16:12 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How does one multiply digits of a number?
« Reply #13 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$

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: How does one multiply digits of a number?
« Reply #14 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