facebook like button

27 April, 2011

program 34: printing a triangle of stars(*)

The need:
     This program takes a whole paragraph as input and calculates total number of characters, words and lines.
The code: 
--------------------------------------------
#include<stdio.h>
main()
{
    int j,i,k;
    printf("Enter the number of stars in the base..");
    scanf("%d",&k);
    for (i=1;i<=k;i++)
    {
      for (j=0;j<i;j++)
      {
         printf ("*  ");
      }
      printf("\n");
    }
}
--------------------------------------------
The approach: 
This program illustrates how we can print in 2 dimensional space(like matrix in next posts). Run the program. This program asks you to enter the number of stars in the base. Give an integer from 1 to 30(so that you can see the output clearly). You can give a bigger integer but the shape of triangle may not be clearly visible at that number. As you can see the output, the program create a right angle triangle of stars. This is done by two nested for loops here. Each run of inner for loop cause a single * to be printed on screen. Each run of outer loop has i number of runs of inner loop. So each run of outer loop prints i number of stars on the screen followed by a new line.

1 comment:

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply