Tuesday 26 August 2014

C Program to display triangle using *



C Program to display triangle using *

This is a C program to display a pyramid (triangle) of stars. In this example, it takes the number of rows required in the triangle and then prints the triangle using * character.

See the source code of the program

/*
            Program to display triangle using *
            input  : number of rows
*/

#include <stdio.h>
int main() {
clrscr();
 int i,j,rows;
 printf("Enter number of rows: ");
 scanf("%d",&rows);

                for(i=1;i<=rows;i++)  {
                                for(j=1;j<=i;j++)    {
                                                printf("* ");
                                }
                                printf("\n");
                }
   
                printf("\nPress any key to continue...");
                getch();
   
                return 0;
}


Output



No comments:

Post a Comment