Home  >  Article  >  Backend Development  >  C++ program to print multiplication table in triangle form

C++ program to print multiplication table in triangle form

WBOY
WBOYforward
2023-09-15 13:21:091484browse

C++ program to print multiplication table in triangle form

To remember some basic multiplication results in tabular or graphical form, use the multiplication table. This article will show you how to generate a multiplication table in C that looks like a right triangle. Triangular notation is effective in the few cases where a large number of results can be easily memorized. In this format, the table is displayed row by row and column by column, with each row containing only the entries that populate that column.

To solve this problem, we need basic loop statements in C. To display the numbers in a triangular fashion, we need nested loops to print each line one by one. We will see how to solve this problem. Let us see the algorithm and implementation for better understanding.

algorithm

  • Get the number of rows of the multiplication table we want, assuming it is n.
  • For i from 1 to n, do the following.
    • For j ranging from 1 to i, do the following - i. Display(i*j).
    • Finish.
  • Finish.
The Chinese translation of

Example

is:

Example

#include <iostream>
using namespace std;

void solve( int n ) {
   int i;
   int j;
   for( i = 1; i <= n; i++ ) {
      for( j = 1; j <= i; j++ ) {
         cout << i * j << " ";
      }
      cout << endl;
   }
}
int main(){
   solve( 8 );
}

Output (using input 8)

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

Output (input 15)

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 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169 
14 28 42 56 70 84 98 112 126 140 154 168 182 196 
15 30 45 60 75 90 105 120 135 150 165 180 195 210 225 

in conclusion

Row I and column j are multiplied in the trigonometric multiplication table. As a result, a multiplication table with an input of 8 will produce 8 rows, where each element is multiplied by 1 to the row number itself. The triangle is formed using two nested loops, which is a very simple method. We also produce triangular designs in the same way.

The above is the detailed content of C++ program to print multiplication table in triangle form. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete