Print multiplication tables by input

In this challenge you've to print multiplication tables by input, Here are some examples:

Input: 2

Output:
0 2  4 6 8  10  12  14  16  18  20

Input: 20

Output: 20 40 60 80 100 120 140 160 180 200


Rules

1. The shortest code in bytes wins.

2. This challenge is a code-golf, It follows code-golf general rules ()

3. If, just if, your code can't print number, you can use letters, Here is an example:

Input: B

Output: B D F H J L N P R T

4. You can choose to start from 0 or your number (like 20). You can choose if put spaces or don't. The challenge is free, just take an input and print multiplication tables.

5. Your output must list the first 10 members of the times table for the given number. You may leave out 0*n.

• Your first example has multiples from 0 to 10, the second from 1 to 10. Can we choose either of these or is one of them a typo? Also, does output have to be space-separated or can we use other list-formats? (If only spaces, the number of spaces is a bit random in your example.) Sep 7, 2016 at 11:55
• You can choose to start from 0 or your number (like 20). You can choose if put spaces or don't. The challenge is free, just take an input and print multiplication tables. Sep 7, 2016 at 11:56
• Welcome to PPCG! Nice to see a pretty simply challenge, since we don't have these enough, although in the future I would add some more information. Like: From the test cases it seems we only need to output 10 numbers, but I don't see this specified. Do we need to support negative input? Why are there two spaces between 2 and 4? Why does the first test case have the 0 in it's output (making it 11 output numbers instead of 10). etc. etc. Also, the Sandbox for proposed challenges is a good place to post first to perfect the challenge Sep 7, 2016 at 12:19
• Under rule 3, what should the output for C be? How about Z?
– Lynn
Sep 7, 2016 at 15:44
• What exactly is the output, the normal rules tend to allow a function to return its output as a list rather than printing them to STDOUT. Sep 7, 2016 at 22:04

MATL, 4 bytes

10:*


Breakdown:

        % Implicit input
10:     % Create a list from 1 2 ... 10
*    % Multiply list by input
% Implicit output


Try it online

C#, 1059667 56 bytes

Now that I know how lambda's work in C#, here is an update to my first answer:

n=>{for(int i=0;i++<10;)System.Console.Write(i*n+" ");};


Saves 11 bytes.

First post, please forgive me for anything I've done wrong. Also, feel free to give me golfing tips, as I haven't really tried it before!

void c(int n){for(int i=0;i++<10;){System.Console.Write(i*n+" ");}}


Ungolfed:

void c(int n)
{
for (int i = 0; i++ < 10 ; )
{
System.Console.Write(i*n+" ");
}
}


Thank you Jonathan Allan, can't add comments yet. And thank you Kevin Cruijssen. I assumed I had to always include the entire program unless the question specified that snippets were allowed. Would I also be able to leave out the System. call to print to console in this case, or are using/imports required then?

• Welcome to PPCG! You can remove the class, only the main function is required by golfing rules :) Sep 7, 2016 at 13:17
• @JonathanAllan Not only that, but by those same rules you can also just make a separate method without Main altogether. I.e. void f(int n){for(int i=0;i++<10;){System.Console.Write(i*n+" ");}} And indeed, welcome to PPCG! Sep 7, 2016 at 13:43
• codegolf.stackexchange.com/q/173/39436 Sep 8, 2016 at 1:34

Jelly, 3 bytes

⁵R×


Test it at TryItOnline
Or first 256 cases, nicely aligned, also at TryItOnline

How?

⁵R× - main link takes one argument, n
⁵   - literal 10
R  - range [1,2,...,10]
× - multiply by input (implicit vectorization)

• I'd love to learn Jelly but half the commands don't render for me, so it'd be useless :D Sep 7, 2016 at 12:21
• @BetaDecay I can't type most of them and they don't render for me in any text editor or on my cmd line :( - they render fine in Firefox on my Windows 7 machine though. Sep 7, 2016 at 12:22
• You and Emigma are winning. Sep 8, 2016 at 13:26
• @BetaDecay - It could help you to download and (re)install DejaVu Sans Mono font (I can now see almost every character in notepad++ and TIO through Firefox is now using it too and displaying every character still) Sep 8, 2016 at 15:22
• Exactly what I thought of, +1. Oct 6, 2016 at 16:58

Clojure, 70 80 bytes

This is my first post on CG, I hope the input is OK:

70 bytes

(defn -main[& a](println(map #(* %(read-string(first a)))(range 10))))


80 bytes

(defn -main[& a](let[n(read-string(first a))](println(map #(* % n)(range 10)))))


The program will read a number as a stdin argument and display the result:

Output

lein run 10
(0 10 20 30 40 50 60 70 80 90)

• Welcome! That's a great first answer! Sep 7, 2016 at 14:28

05AB1E, 3 bytes

TL*


Explanation

T    # literal predefined  as 10
L   # 1-based range: [1,2,3,4,5,6,7,8,9,10]
*  # multiply with input


Try it online!

• You and Jonathan Allan are winning Sep 8, 2016 at 13:26

Perl, 19 bytes

Includes +1 for -n

Run with the input on STDIN:

perl -M5.1010 -n table.pl <<< 8


table.pl:

say$_*$for/$/..10  • You probably meant -n ? or did I miss something? – Dada Sep 7, 2016 at 20:07 • @Dada: right, conflated it with another version. Fixed Sep 7, 2016 at 22:44 Haskell, 16 bytes (<$>[1..10]).(*)


Usage example: (<$>[1..10]).(*)$ 4 -> [4,8,12,16,20,24,28,32,36,40].

Pointfree version of: f n = map (n*) [1..10].

• What does <$> do? Sep 9, 2016 at 1:30 • @Cyoce: <$> is an infix version of fmap (or map when used with a list), i.e. it applied the function given as it's 1st argument to every element of the list. func <$> list= fmap func list= map func list. – nimi Sep 9, 2016 at 4:56 Jellyfish, 8 bytes p*r11 i  Try it online! Quite simple: r11 gives the list [0, 1, ..., 9, 10], i reads the input, * multiplies them and p prints the resulting list. R, 11 bytes scan()*0:10  30 char. PHP, 34 bytes (34 bytes) for(;$i++<10;)echo$i*$argv[1].' ';


(34 bytes)

for(;++$i%11;)echo$i*$argv[1].' ';  (34 bytes) while($i++<10)echo$i*$argv[1].' ';


(35 bytes)

for(;$i++<10;)echo' '.$a+=$argv[1];  (41 40 bytes) <?=join(' ',range(0,10*$a=$argv[1],$a));


<?=join(' ',range($a=$argv[1],10*$a,$a));


(44 bytes)

foreach(range(1,10)as$i)echo$i*$argv[1].' ';  • The one using range() with $step can be shorter if you start it from 0: <?=join(' ',range(0,10*$a=$argv[1],$a));. Sep 7, 2016 at 13:27 • The rule allowing to start from 0 was not set when I did it, but you're right; I update this one. Sep 7, 2016 at 13:47 J, 8 bytes (i.10)&*  This is the range from 0 to 9 inclusive (i.10) bonded (&) wit the multiplication function (*). This starts at zero. Test cases  k =: (i.10)&* k 2 0 2 4 6 8 10 12 14 16 18 k 10 0 10 20 30 40 50 60 70 80 90 k"0 i.10 0 0 0 0 0 0 0 0 0 0 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  Zsh, 19 characters echo {0..${1}0..$1}  Sample run: (This is the interactive way to run it, equivalent with zsh scriptfile.sh 20.) ~ % set -- 20 ~ % echo {0..${1}0..$1} 0 20 40 60 80 100 120 140 160 180 200  Python 3, 5233 30 bytes lambda n:list(range(0,11*n,n))  3 bytes saved thanks to @manatwork Formatting the output is visibly not necessary • Borrow from my shell answer: lambda n:" ".join(map(str,range(0,n*11,n))) Sep 7, 2016 at 12:10 • @manatwork I use Python 3 Sep 7, 2016 at 12:19 • You use Python 3 but you can save 6 bytes by using Python 2: lambda n:range(0,11*n,n) Sep 7, 2016 at 13:03 • Author AFK for a year. new answer 26 bytes. May 4, 2021 at 17:59 Mata, 15 29 Bytes args i mata A=1..10 B=i'*A B  Mata is the matrix programming language in the Stata commercial statistics package. Code creates a matrix, multiplies by the input (2 in this case) and the outputs the new matrix Output  1 2 3 4 5 6 7 8 9 10 +---------------------------------------------------+ 1 | 2 4 6 8 10 12 14 16 18 20 | +---------------------------------------------------+  • How is it taking input? It needs to be reusable too. Sep 7, 2016 at 13:50 • OK, have edited to clarify receipt of input Sep 7, 2016 at 14:14 Pure Bash, 18 echo$[{0..10}*$1]  The input is taken as a command-line parameter. • Good & great ^_^ Oct 17, 2016 at 6:11 Stata, 46 bytes args i set obs i' gen x=_n gen y=x*i' list y  Output For i=15  +-----+ | y | |-----| 1. | 15 | 2. | 30 | 3. | 45 | 4. | 60 | 5. | 75 | |-----| 6. | 90 | 7. | 105 | 8. | 120 | 9. | 135 | 10.| 150 | |-----| 11.| 165 | 12.| 180 | 13.| 195 | 14.| 210 | 15.| 225 |  • There are a few extra shorthands you can use here: ob for obs, g for gen, and l for list. Also, is it possible to have x be _n*i' instead of using two variables? I'd never seen args before in STATA. Thanks for showing me something new! Feb 17, 2017 at 5:08 Cheddar, 20 bytes n->(|>11).map(n&(*))  Yay for functional \o/ I don't think this needs explanation but if you'd like to me add one just ask :) • I should learn Cheddar. And what does n&(*) do? I'm assuming (*) means the same thing it means I'm haskell, but what does & do in that context? Sep 9, 2016 at 6:31 • @Cyoce it's bonding Sep 16, 2016 at 14:07 Java 7, 61 57 bytes void c(int n){for(int i=0;i++<10;)System.out.print(i*n);}  Ungolfed & test cases: Try it here. class M{ static void c(int n){ for(int i = 0; i++ < 10; ){ System.out.print(i*n); } } public static void main(String[] a){ c(2); System.out.println(); c(20); } }  Output: 2468101214161820 20406080100120140160180200  • Spaces are optional, System.out.print(i*n); would save 4 bytes Sep 8, 2016 at 8:34 • @CameronD17 Ah, that rule has been added after I made this answer, but thanks for mentioning. I've removed it. Sep 8, 2016 at 8:38 JavaScript (ES6), 33 31 bytes f=(x,i=11)=>--i&&f(x,i)+" "+x*i  It's a recursive solution. T-SQL 61 bytes Replace n with the number for which table needs to be populated. Demo SELECT TOP 11 number*n FROM master..spt_values WHERE type='P'  spt_value is a undocumented table in SQL Server, you can read more about this table in I hope someone will come up with better TSQL solution. Scala, 24 bytes (n:Int)=>0 to 10 map(n*)  Explanation: (n:Int)=> //define an anonymous function 0 to 10 //create a range from 0 to 10 map(n*) //multiply each element by the input  Brachylog, 12 bytes ,10y:[?]z:*a  Try it online! I need to implement that I * [A, B, C] = [I*A, I*B, I*C] Explanation ,10y The range [0, 1, …, 10] :[?]z The list [[0, Input], [1, Input], …, [10, Input]] :*a The list [0*Input, 1*Input, …, 10*Input]  brainf***, 84 bytes ,[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++<<<<<<<<<<-]>[.>]  Expects input as a single byte (as BF can only operate on numbers up to 255) and returns results as single bytes. Some values may look like ASCII, but they should not be treated as such; look at the decimal representation of the returned bytes. Try it online! • You can save 5 bytes by doing >,[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++[<]>-]>[.>] – Jeff Sep 7, 2016 at 12:47 • @Jeff Unfortunately, that breaks any input that's higher than 25 because the byte will overflow, resetting to 0. The overflow 0s get matched by [<], which makes the entire process loop infinitely. Sep 7, 2016 at 13:19 • Right, but overflowing bytes result in incorrect outputs with your code anyway, don't they? – Jeff Sep 7, 2016 at 13:31 • @Jeff They're correct outputs, they're just operating in mod 255. Sep 7, 2016 at 19:10 JavaScript, 42 bytes function f(a){for(i=0;i<11;i++)alert(i*a)}  • I don't know JS very well, can you move the increment into the test portion of the for? Sep 7, 2016 at 12:26 • I edited it. It's ok now. Sep 7, 2016 at 12:33 • It alerts now with alert(). Sep 7, 2016 at 12:34 • It was alerting for me without alert: here - I take that back, need to click "run" not just "try" to reload Sep 7, 2016 at 12:35 • @JonathanAllan What? Sep 7, 2016 at 12:36 MATLAB, 12 bytes @(x)x*[1:10]  Not really much to it. An anonymous function that takes x as input and multiplies it by the vector [1:10]. Displays as ans = 2 4 6 ... Also works in Octave. Try it online. PowerShell v2+, 23 bytes param($n)1..10|%{$_*$n}


Takes input via command-line argument, loops over the range 1 to 10, each loop placing that number *\$n on the pipeline. Output via implicit Write-Output at end of program execution results in newline separated values.

PS C:\Tools\Scripts\golfing> .\multiplication-table.ps1 2
2
4
6
8
10
12
14
16
18
20

PS C:\Tools\Scripts\golfing> .\multiplication-table.ps1 20
20
40
60
80
100
120
140
160
180
200


C89, 44 bytes

k;main(n){for(k=n*11;k-=n;)printf("%d ",k);}


Ungolfed:

k;
main(n)
{
for (k=n*11 ; k-=n ;)
printf("%d ", k);
}


Compile and run with (input 4)

gcc -ansi main.c && ./a.out 2 3 4


Output

40 36 32 28 24 20 16 12 8 4


Demo

Pyke, 5 bytes

TSQm*


Try it here!

Or TQm* if allowed to do numbers 0-9 rather than 1-10

Or TL* if we're going non-competitive.

Javascript (ES6), 34 31 bytes

a=>{for(i=0;i<11;)alert(++i*a)}


Saved 3 bytes thanks to grizzly.

• At the very least you don't need the parentheses around the a, but I think it's also possible to be creative as to the position of the ++.
– Neil
Sep 7, 2016 at 15:24

Cubix, 24 bytes

;;..I1.N-?W.;;)W@/So..*O


Cubix is a 2-dimensional, stack-based esolang. Cubix is different from other 2D langs in that the source code is wrapped around the outside of a cube.

Test it online! Note: you'll have to copy-paste the code, and there's a 50 ms delay between iterations.

Explanation

The first thing the interpreter does is figure out the smallest cube that the code will fit onto. In this case, the edge-length is 1. Then the code is padded with no-ops . until all six sides are filled. Whitespace is removed before processing, so this code is identical to the above:

    ; ;
. .
I 1 . N - ? W .
; ; ) W @ / S o
. .
* O

• Here's a slightly shorter one using the new stack options I.0.WrN;-!@vrW>r)*O;o` Oct 13, 2016 at 23:06