In this article, we will show how to write a C program to print the multiplication numbers in a right angled triangle row and column using for loop, while loop, and functions.
The below example allows the user to enter the total number of rows, and the nested for loop iterates them from start to end. Next, the program will print the multiplication numbers in every right angled triangle row.
#include <stdio.h>
int main()
{
int rows;
printf("Enter Rows = ");
scanf("%d",&rows);
for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j <= i; j++ )
{
printf("%d ", i * j);
}
printf("\n");
}
return 0;
}
Enter Rows = 12
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
Instead of a for loop, this program uses a while loop to iterate the right angled triangle rows and columns and prints the multiplication numbers in every row. For more number programs, click here.
#include <stdio.h>
int main()
{
int rows;
printf("Enter Rows = ");
scanf("%d",&rows);
int i = 1 ;
while(i <= rows)
{
int j = 1 ;
while ( j <= i)
{
printf("%d ", i * j);
j++;
}
printf("\n");
i++;
}
return 0;
}
Enter Rows = 11
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
In this C program, we create a RightTriangleMultiplicationNumbers function that accepts the user entered rows and prints the right angled triangle of multiplication numbers on each row.
#include <stdio.h>
void RightTriangleMultiplicationNumbers(int rows)
{
for (int i = 1 ; i <= rows; i++ )
{
for (int j = 1 ; j <= i; j++ )
{
printf("%d ", i * j);
}
printf("\n");
}
}
int main()
{
int rows;
printf("Enter Rows = ");
scanf("%d",&rows);
RightTriangleMultiplicationNumbers(rows);
return 0;
}