C Program To Print Triangle
Last Updated :
02 Aug, 2022
Here, we will see how to print a triangle using the C program
Input:
5
Output:
*
* *
* * *
* * * *
* * * * *
Approach:
The approach is very simple. We just execute a nested loop, and only print characters when the inner loop is executed after that just change the line.
Example:
C
#include <stdio.h>
int main()
{
int n = 6;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++) {
printf ( "*" );
}
printf ( "\n" );
}
return 0;
}
|
Output
*
**
***
****
*****
******
Time Complexity: O(n2)
Auxiliary Space: O(1), No extra Space is used.
Share your thoughts in the comments
Please Login to comment...