How to Print the Multiplication Table in Triangular Form in Golang?

Go ProgrammingServer Side ProgrammingProgramming

Cyber Security : Go from Zero to Hero

Most Popular

60 Lectures 5.5 hours

Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

In this tutorial, we are going to generate the Multiplication Table in Triangular Form using Golang programming. To achieve this we have two ways, one using the nested loops and another using the recursion concept.

Using for nested loops to generate the Multiplication Table in Triangular Form

Algorithm

• Step 1 − Declaring the int type variable in which we will store the number till which we have to generate the multiplication table in triangular form.

• Step 2 − Initialize the number with the respective value till which we have to generate the multiplication table in triangular form.

• Step 3 − Print the multiplication table in triangular form using the nested for loops.

Example 1

In this example, we are going to use the for nested loops to generate the Multiplication Table in Triangular Form till the respective number.

package main

// fmt package provides the function to print anything
import (
"fmt"
)

func main() {
// declaring the variable till which we have to print
// Multiplication Table in Triangular Form
var number int

// initializing the variable with the respective number
number = 10
fmt.Println("Multiplication Table in Triangular Form for the number", number, "using for loop.")

// running the for loop from 1 till
for i := 1; i <= number; i++ {

// Running an inner loop from 1 till the index of the outer loop
for j := 1; j <= i; j++ {

// multiplying the value of the outer loop index with
// inner loop index
fmt.Print(i*j, " ")
}

// adding the new line in the end
fmt.Println()
}
}


Output

Multiplication Table in Triangular Form for the number 10 using for loop.
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100


Using the concept of recursion to generate the table of the respective number.

Algorithm

• Step 1 − Declaring the int type variable in which we will store the number till which we have to generate the multiplication table in triangular form.

• Step 2 − Initialize the number with the respective value till which we have to generate the multiplication table in triangular form.

• Step 3 − Print the multiplication table in triangular form using the recursive function.

Example 2

In this example, we are going to use the concept of recursion to generate the table of the respective number.

package main

// fmt package provides the function to print anything
import (
"fmt"
)

// this is a function with three parameters of int type
func recursiveTriangularTableGenerator(number, i, j int) {

// if the value of i becomes greater than the number
// then stopping the recursion calls
if i > number {
return
}

if j <= i {
// multiplying i and j and then calling the function again
// by increasing the value of j
fmt.Print(i*j, " ")
recursiveTriangularTableGenerator(number, i, j+1)

} else {
// if the value of j becomes greater than i then increasing the i
// passing the value of j as 1
fmt.Println()
recursiveTriangularTableGenerator(number, i+1, 1)
}
}

func main() {
// declaring the variable till which we have to print
// Multiplication Table in Triangular Form
var number int

// initializing the variable with the respective number
number = 10
fmt.Println("Multiplication Table in Triangular Form for the number", number, "using recursion.")

// calling the recursive function with three parameters
recursiveTriangularTableGenerator(number, 1, 1)
}


Output

Multiplication Table in Triangular Form for the number 10 using recursion.
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100


Conclusion

These are the two ways to generate the Multiplication Table in the Triangular Form till the particular number in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about Golang you can explore these tutorials.

Updated on 30-Sep-2022 08:18:29

Advertisements