11
\$\begingroup\$

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.

\$\endgroup\$
5
  • 10
    \$\begingroup\$ 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.) \$\endgroup\$ Sep 7, 2016 at 11:55
  • \$\begingroup\$ 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. \$\endgroup\$
    – Rizze
    Sep 7, 2016 at 11:56
  • 4
    \$\begingroup\$ 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 \$\endgroup\$ Sep 7, 2016 at 12:19
  • \$\begingroup\$ Under rule 3, what should the output for C be? How about Z? \$\endgroup\$
    – Lynn
    Sep 7, 2016 at 15:44
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Sep 7, 2016 at 22:04

59 Answers 59

10
\$\begingroup\$

MATL, 4 bytes

10:*

Breakdown:

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

Try it online

\$\endgroup\$
10
\$\begingroup\$

C#, 105 96 67 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?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to PPCG! You can remove the class, only the main function is required by golfing rules :) \$\endgroup\$ Sep 7, 2016 at 13:17
  • 1
    \$\begingroup\$ @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! \$\endgroup\$ Sep 7, 2016 at 13:43
  • 1
    \$\begingroup\$ codegolf.stackexchange.com/q/173/39436 \$\endgroup\$
    – fede s.
    Sep 8, 2016 at 1:34
9
\$\begingroup\$

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)
\$\endgroup\$
5
  • \$\begingroup\$ I'd love to learn Jelly but half the commands don't render for me, so it'd be useless :D \$\endgroup\$
    – Beta Decay
    Sep 7, 2016 at 12:21
  • \$\begingroup\$ @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. \$\endgroup\$ Sep 7, 2016 at 12:22
  • \$\begingroup\$ You and Emigma are winning. \$\endgroup\$
    – Rizze
    Sep 8, 2016 at 13:26
  • \$\begingroup\$ @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) \$\endgroup\$ Sep 8, 2016 at 15:22
  • \$\begingroup\$ Exactly what I thought of, +1. \$\endgroup\$ Oct 6, 2016 at 16:58
8
\$\begingroup\$

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)
\$\endgroup\$
1
  • 5
    \$\begingroup\$ Welcome! That's a great first answer! \$\endgroup\$
    – mbomb007
    Sep 7, 2016 at 14:28
7
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ You and Jonathan Allan are winning \$\endgroup\$
    – Rizze
    Sep 8, 2016 at 13:26
6
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ You probably meant -n ? or did I miss something? \$\endgroup\$
    – Dada
    Sep 7, 2016 at 20:07
  • \$\begingroup\$ @Dada: right, conflated it with another version. Fixed \$\endgroup\$
    – Ton Hospel
    Sep 7, 2016 at 22:44
5
\$\begingroup\$

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].

\$\endgroup\$
2
  • \$\begingroup\$ What does <$> do? \$\endgroup\$
    – Cyoce
    Sep 9, 2016 at 1:30
  • \$\begingroup\$ @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. \$\endgroup\$
    – nimi
    Sep 9, 2016 at 4:56
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

R, 11 bytes

scan()*0:10

30 char.

\$\endgroup\$
4
\$\begingroup\$

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].' ';
\$\endgroup\$
2
  • \$\begingroup\$ The one using range() with $step can be shorter if you start it from 0: <?=join(' ',range(0,10*$a=$argv[1],$a));. \$\endgroup\$
    – manatwork
    Sep 7, 2016 at 13:27
  • \$\begingroup\$ The rule allowing to start from 0 was not set when I did it, but you're right; I update this one. \$\endgroup\$
    – Crypto
    Sep 7, 2016 at 13:47
4
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

Python 3, 52 33 30 bytes

lambda n:list(range(0,11*n,n))

3 bytes saved thanks to @manatwork

Formatting the output is visibly not necessary

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Borrow from my shell answer: lambda n:" ".join(map(str,range(0,n*11,n))) \$\endgroup\$
    – manatwork
    Sep 7, 2016 at 12:10
  • \$\begingroup\$ @manatwork I use Python 3 \$\endgroup\$ Sep 7, 2016 at 12:19
  • 1
    \$\begingroup\$ You use Python 3 but you can save 6 bytes by using Python 2: lambda n:range(0,11*n,n) \$\endgroup\$ Sep 7, 2016 at 13:03
  • \$\begingroup\$ Author AFK for a year. new answer 26 bytes. \$\endgroup\$
    – Gulzar
    May 4, 2021 at 17:59
3
\$\begingroup\$

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  |
    +---------------------------------------------------+
\$\endgroup\$
2
  • 1
    \$\begingroup\$ How is it taking input? It needs to be reusable too. \$\endgroup\$ Sep 7, 2016 at 13:50
  • 1
    \$\begingroup\$ OK, have edited to clarify receipt of input \$\endgroup\$
    – f1rstguess
    Sep 7, 2016 at 14:14
3
\$\begingroup\$

Pure Bash, 18

echo $[{0..10}*$1]

The input is taken as a command-line parameter.

\$\endgroup\$
1
  • \$\begingroup\$ Good & great ^_^ \$\endgroup\$
    – ABcDexter
    Oct 17, 2016 at 6:11
3
\$\begingroup\$

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 |
\$\endgroup\$
1
  • \$\begingroup\$ 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! \$\endgroup\$
    – bmarks
    Feb 17, 2017 at 5:08
3
\$\begingroup\$

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 :)

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$
    – Cyoce
    Sep 9, 2016 at 6:31
  • \$\begingroup\$ @Cyoce it's bonding \$\endgroup\$
    – Downgoat
    Sep 16, 2016 at 14:07
3
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Spaces are optional, System.out.print(i*n); would save 4 bytes \$\endgroup\$
    – CameronD17
    Sep 8, 2016 at 8:34
  • \$\begingroup\$ @CameronD17 Ah, that rule has been added after I made this answer, but thanks for mentioning. I've removed it. \$\endgroup\$ Sep 8, 2016 at 8:38
3
\$\begingroup\$

JavaScript (ES6), 33 31 bytes

f=(x,i=11)=>--i&&f(x,i)+" "+x*i

It's a recursive solution.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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]
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ You can save 5 bytes by doing >,[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++[<]>-]>[.>] \$\endgroup\$
    – Jeff
    Sep 7, 2016 at 12:47
  • \$\begingroup\$ @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. \$\endgroup\$
    – Steven H.
    Sep 7, 2016 at 13:19
  • \$\begingroup\$ Right, but overflowing bytes result in incorrect outputs with your code anyway, don't they? \$\endgroup\$
    – Jeff
    Sep 7, 2016 at 13:31
  • 1
    \$\begingroup\$ @Jeff They're correct outputs, they're just operating in mod 255. \$\endgroup\$
    – Steven H.
    Sep 7, 2016 at 19:10
2
\$\begingroup\$

JavaScript, 42 bytes

function f(a){for(i=0;i<11;i++)alert(i*a)}
\$\endgroup\$
7
  • \$\begingroup\$ I don't know JS very well, can you move the increment into the test portion of the for? \$\endgroup\$ Sep 7, 2016 at 12:26
  • \$\begingroup\$ I edited it. It's ok now. \$\endgroup\$
    – Rizze
    Sep 7, 2016 at 12:33
  • \$\begingroup\$ It alerts now with alert(). \$\endgroup\$
    – Rizze
    Sep 7, 2016 at 12:34
  • \$\begingroup\$ It was alerting for me without alert: here - I take that back, need to click "run" not just "try" to reload \$\endgroup\$ Sep 7, 2016 at 12:35
  • 1
    \$\begingroup\$ @JonathanAllan What? \$\endgroup\$
    – Rizze
    Sep 7, 2016 at 12:36
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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 

Test it

Demo

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
0
2
\$\begingroup\$

Javascript (ES6), 34 31 bytes

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

Saved 3 bytes thanks to grizzly.

\$\endgroup\$
1
  • \$\begingroup\$ 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 ++. \$\endgroup\$
    – Neil
    Sep 7, 2016 at 15:24
2
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Here's a slightly shorter one using the new stack options I.0.WrN;-!@vrW>r)*O;o \$\endgroup\$
    – MickyT
    Oct 13, 2016 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.