Pr.Pg Next Pg

C pyramid programs of type 5 tutorials

 

Example: Program to print rhombus of numbers

 Sam Madam

/*WAP to print a pyramid

1

2 1 2

3 2 1 2 3

4 3 2 1 2 3 4

5 4 3 2 1 2 3 4 5

4 3 2 1 2 3 4

3 2 1 2 3

2 1 2

1

*/

#include <stdio.h>

#include <conio.h>

void main()

{

for (int f=1;f<=5;f++) // for 5 loops

{

for (int sp=5-f;sp>=0;sp--) //for spaces

{

printf(" ");

}

 

for (int s=f;s>=1;s--) // to display values

{

printf("%d ",s);

}

for (int x=2;x<=f;x++) //2nd part right side

{

printf("%d ",x);

}

printf("\n");

}

for (f=4;f>=1;f--) // for 5 loops

{

for (int sp=5-f;sp>=0;sp--) //for spaces

{

printf(" ");

}

 

for ( int s=f;s>=1;s--) // to display values

{

printf("%d ",s);

}

for ( int x=2;x<=f;x++) //2nd part right side

{

printf("%d ",x);

}

printf("\n");

}

getch();

}

Output

 

1

2 1 2

3 2 1 2 3

4 3 2 1 2 3 4

5 4 3 2 1 2 3 4 5

4 3 2 1 2 3 4

3 2 1 2 3

2 1 2

1

 

 

 

Example: Program to print rhombus of numbers

Sam Madam

/*WAP to print a rhombus

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 2 1

1 2 1

1

*/

 

#include <stdio.h>

#include <conio.h>

void main()

{

int i,s,j,k;

for (i=1;i<=4;i++)

{

for (s=1;s<=4-i;s++)

{

printf(" "); //double spaces

}

for (j=1;j<=i;j++)

{

printf("%d ",j); //printing front values

}

for (k=i-1;k>=1;k--)

{

printf("%d ",k); //printing back values

}

printf("\n");

}

for (i=3;i>=1;i--)

{

for (s=1;s<=4-i;s++)

{

printf(" "); //double spaces

}

for (j=1;j<=i;j++)

{

printf("%d ",j);

}

for (k=i-1;k>=1;k--)

{

printf("%d ",k);

}

printf("\n");

}

getch();

}

Output

 

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 2 1

1 2 1

1

 

 

Example: Program to print a XMAS tree

Sam Madam

/*WAP to print a XMAS tree

/\

/ \

/ \

/ \

-------

" "

" "

" "

*/

#include <stdio.h>

#include <conio.h>

void main()

{

int n,s,x;

 

for (n=0;n<=3;n++)

{

for (s=3;s>=n;s--) // for front spaces

printf(" ");

 

printf("/");

 

for (x=0;x<(n*2);x++) // for internal spaces 0,2,4,6..

printf(" ");

 

printf("\\"); // printing escape seq. for backspace

printf("\n");

}

printf(" -------\n");

for (n=0;n<=2;n++)

printf(" \" \" \n"); // printing " with escape seq,

}

Output

/\

/ \

/ \

/ \

-------

" "

" "

" "

Pr.Pg border                                              Next Pg