0
0
GoProgramBeginner · 2 min read

Go Program to Print Multiplication Table

Use a simple for loop in Go to print the multiplication table, like for i := 1; i <= 10; i++ { fmt.Printf("%d x %d = %d\n", n, i, n*i) } where n is the number.
📋

Examples

Input5
Output5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Input1
Output1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10
Input10
Output10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60 10 x 7 = 70 10 x 8 = 80 10 x 9 = 90 10 x 10 = 100
🧠

How to Think About It

To print a multiplication table, think of a number you want to multiply. Then, repeat multiplying that number by each number from 1 to 10. For each step, show the multiplication and the result. This uses a simple counting loop that goes from 1 to 10.
📐

Algorithm

1
Get the number for which to print the multiplication table.
2
Start a loop from 1 to 10.
3
In each loop step, multiply the number by the loop counter.
4
Print the multiplication expression and the result.
5
Repeat until the loop reaches 10.
💻

Code

go
package main

import "fmt"

func main() {
    n := 5
    for i := 1; i <= 10; i++ {
        fmt.Printf("%d x %d = %d\n", n, i, n*i)
    }
}
Output
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
🔍

Dry Run

Let's trace the multiplication table for 5 through the code.

1

Set number

n = 5

2

Start loop

i = 1 to 10

3

Multiply and print

For i=1: 5 x 1 = 5 For i=2: 5 x 2 = 10 ... up to i=10

iExpressionResult
15 x 15
25 x 210
35 x 315
45 x 420
55 x 525
65 x 630
75 x 735
85 x 840
95 x 945
105 x 1050
💡

Why This Works

Step 1: Loop from 1 to 10

The for loop runs 10 times, once for each multiplier from 1 to 10.

Step 2: Multiply number by loop counter

In each loop, multiply the chosen number n by the current loop index i.

Step 3: Print formatted output

Use fmt.Printf to show the multiplication expression and result in a clear format.

🔄

Alternative Approaches

Using nested loops for full table
go
package main

import "fmt"

func main() {
    for n := 1; n <= 10; n++ {
        for i := 1; i <= 10; i++ {
            fmt.Printf("%d x %d = %d\t", n, i, n*i)
        }
        fmt.Println()
    }
}
Prints the full multiplication table from 1 to 10 in a grid format, useful for learning all tables at once.
Using a function to print table
go
package main

import "fmt"

func printTable(n int) {
    for i := 1; i <= 10; i++ {
        fmt.Printf("%d x %d = %d\n", n, i, n*i)
    }
}

func main() {
    printTable(7)
}
Encapsulates the logic in a function for reuse and cleaner main code.

Complexity: O(1) time, O(1) space

Time Complexity

The loop runs exactly 10 times, so time is constant, O(1), regardless of input.

Space Complexity

No extra memory grows with input; only a few variables are used, so space is O(1).

Which Approach is Fastest?

All approaches run in constant time for fixed 10 iterations; using a function adds clarity but no speed difference.

ApproachTimeSpaceBest For
Single loop for one numberO(1)O(1)Simple, focused output
Nested loops for full tableO(1)O(1)Complete multiplication tables
Function encapsulationO(1)O(1)Reusable and clean code
💡
Use fmt.Printf with format specifiers to neatly align multiplication output.
⚠️
Beginners often forget to increment the loop counter or set the correct loop range, causing infinite loops or missing output.