Product of decimal digits of n: Difference between revisions

From Rosetta Code
Content added Content deleted
(added whitespace and highlighting, and also added articles ("the"), changed the concept of "n" to a "positive integer n", as that what the author's entry used.)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 8: Line 8:
Find the product of the decimal digits of a positive integer &nbsp; '''n''', &nbsp; where '''n <= 100'''
Find the product of the decimal digits of a positive integer &nbsp; '''n''', &nbsp; where '''n <= 100'''
<br><br>
<br><br>

=={{header|REXX}}==
<lang rexx>/*REXX pgm finds positive integers when shown in hex that can't be written with dec digs*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n = 100 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 10 /*width of a number in any column. */
title= ' the product of the decimal digits of N, where N < ' n
say ' index │'center(title, 1 + cols*(w+1) ) /*display the title for the output. */
say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */
$=; idx= 1 /*list of products (so far); IDX=index.*/
do #=1 for n; L= length(#) /*find products of the dec. digs of J. */
p= left(#, 1) /*use first digit as the product so far*/
do j=2 for L-1 until p==0 /*add an optimization when product is 0*/
p= p * substr(#, j, 1) /*multiply the product by the next dig.*/
end /*j*/
$= $ right(p, w) /*add the product ───► the $ list. */
if #//cols \== 0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*#*/ /*stick a fork in it, we're all done. */

if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─') /*display the foot sep for output. */</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
index │ the product of the decimal digits of N, where N < 100
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 1 2 3 4 5 6 7 8 9 0
11 │ 1 2 3 4 5 6 7 8 9 0
21 │ 2 4 6 8 10 12 14 16 18 0
31 │ 3 6 9 12 15 18 21 24 27 0
41 │ 4 8 12 16 20 24 28 32 36 0
51 │ 5 10 15 20 25 30 35 40 45 0
61 │ 6 12 18 24 30 36 42 48 54 0
71 │ 7 14 21 28 35 42 49 56 63 0
81 │ 8 16 24 32 40 48 56 64 72 0
91 │ 9 18 27 36 45 54 63 72 81 0
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
</pre>


=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 20:35, 25 June 2021

     

Task

Find the product of the decimal digits of a positive integer   n,   where n <= 100

REXX

<lang rexx>/*REXX pgm finds positive integers when shown in hex that can't be written with dec digs*/ parse arg n cols . /*obtain optional argument from the CL.*/ if n== | n=="," then n = 100 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 10 /*width of a number in any column. */

                    title= ' the product of the decimal digits of  N,  where  N  < '  n

say ' index │'center(title, 1 + cols*(w+1) ) /*display the title for the output. */ say '───────┼'center("" , 1 + cols*(w+1), '─') /* " a sep " " " */ $=; idx= 1 /*list of products (so far); IDX=index.*/

   do #=1  for n;   L= length(#)                /*find products of the dec. digs of J. */
   p= left(#, 1)                                /*use first digit as the product so far*/
                    do j=2  for L-1  until p==0 /*add an optimization when product is 0*/
                    p= p * substr(#, j, 1)      /*multiply the product by the next dig.*/
                    end   /*j*/
   $= $ right(p, w)                             /*add the product  ───►  the  $  list. */
   if #//cols \== 0  then iterate               /*have we populated a line of output?  */
   say center(idx, 7)'│'  substr($, 2);    $=   /*display what we have so far  (cols). */
   idx= idx + cols                              /*bump the  index  count for the output*/
   end   /*#*/                                  /*stick a fork in it,  we're all done. */

if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say '───────┴'center("" , 1 + cols*(w+1), '─') /*display the foot sep for output. */</lang>

output   when using the default inputs:
 index │                           the product of the decimal digits of  N,  where  N  <  100
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │          1          2          3          4          5          6          7          8          9          0
  11   │          1          2          3          4          5          6          7          8          9          0
  21   │          2          4          6          8         10         12         14         16         18          0
  31   │          3          6          9         12         15         18         21         24         27          0
  41   │          4          8         12         16         20         24         28         32         36          0
  51   │          5         10         15         20         25         30         35         40         45          0
  61   │          6         12         18         24         30         36         42         48         54          0
  71   │          7         14         21         28         35         42         49         56         63          0
  81   │          8         16         24         32         40         48         56         64         72          0
  91   │          9         18         27         36         45         54         63         72         81          0
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────

Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Product of decimal digits of n:" + nl

row = 0 limit = 100

for n = 1 to limit

   prod = 1
   strn = string(n)
   for m = 1 to len(strn)
       prod = prod * number(strn[m])
   next    
   see "" + prod + " "
   row = row + 1
   if row%5 = 0
      see nl
   ok

next

see "done..." + nl </lang>

Output:
working...
Product of decimal digits of n:
1 2 3 4 5 
6 7 8 9 0 
1 2 3 4 5 
6 7 8 9 0 
2 4 6 8 10 
12 14 16 18 0 
3 6 9 12 15 
18 21 24 27 0 
4 8 12 16 20 
24 28 32 36 0 
5 10 15 20 25 
30 35 40 45 0 
6 12 18 24 30 
36 42 48 54 0 
7 14 21 28 35 
42 49 56 63 0 
8 16 24 32 40 
48 56 64 72 0 
9 18 27 36 45 
54 63 72 81 0 
done...