Saturday 8 December 2012

Pyramid pattern in c program | to print pyramid | comments with for loop


In this program we are going to learn how to print  triangle in pyramid  pattern in c program using two for loop.so ,Basically there are two types of pyramid
1->Floyd triangle
2->Pascal triangle.

structure for pyramid

                                t
                               t  t
                             t t  t  t
                            t  t  t  t  t
void main ()// source code
{

int i,j,n;
clrscr();
printf("Enter number of lines\n");
scanf("%d",&n);
for(i=1;i<=n;i++)//loop for number of lines
{
 for(j=0;j<40-i;j++) //for printing spaces
{
printf(" ");
}
 for(j=i;j<=2*i-1;j++)//for printing trinagle on left half
 {
 printf("%d",j%10);
}
for(j=2*i-2;j>=i;j--)//for printing triangle on right half
{
printf("%d",j%10);
}
printf("\n");
}
getch();
}

another way also is there

Another pattern  with source code
* * * * *
* * * *
* * *
* *
*
*/

#include<stdio.h>
int main()
{
int i,j,n;

printf("Enter no. of lines: ");
scanf( "%d",&n);


//for loop for number of lines
for(i=n;i>=1;i--)
{

//for printing stars
for(j=1;j<=i;j++)
 {
printf("* ");
 }
 printf("\n");
}
return 0;
}

so , basically we need two for loops .first for loop for printing number of lines .
How many lines do you need .
then after print one simple star we need to give space for that we need third for loop.
After that triagle for left half and right half.
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: