C program to print *'s in triangle format


The source code for given pattern is shown below:

*
* *
* * *
* * * *
* * * * *
* * * * * *
...
...

Program:

#include <stdio.h>
#include <conio.h>
int main() 
{
    int i, j, rows;
    clrscr();
    printf ("Enter the number of rows: ");
    scanf ("%d", &rows);
    printf("\n--------------------");
    for (i=1; i<=rows; ++i) 
      {
           for (j=1; j<=i; ++j)
              {
                   printf("* ");
              }
          printf("\n");
      }
   printf("--------------------");
   return 0;
   
}
Output:
Enter the number of rows: 4
--------------------
*
* *
* * *
* * * *
--------------------
Explanation:
Import the required header files like stdio and conio. Any program in C should start with a main(), initialize the variables required for the program, clrscr() used to clear the screen and printf() will display the given message on the display screen. The user input is accepted by a scanf() function and the input is stored in the variables called rows.Logic to print the required output is executed and the output is displayed on the display screen.

Post a Comment

0 Comments