Open In App

C Program To Print Triangle

Improve
Improve
Like Article
Like
Save
Share
Report

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




// C program to print a triangle
#include <stdio.h>
  
// Driver code
int main()
{
    int n = 6;
  
    // ith row has i elements
    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.


Last Updated : 02 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads