Pr.Pg Next Pg

C pyramid examples of type 2 tutorials

 

Example: Program to print pyramid of star

 Sam Madam

/*WAP to print a pyramid

*

* *

* * *

* * * *

* * * * *

*/

#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("* ");

}

 

printf("\n"); // for line break

}

getch();

}

 

Outpu

*

* *

* * *

* * * *

* * * * *

 

 

Example: Program to print pyramid of numbers.

Sam Madam

 

/* Program to print pyramid of numbers.

1

2 1

3 2 1

4 3 2 1

5 4 3 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);

}

printf("\n");

// for line break

}

getch();

}

Output

 

1

2 1

3 2 1

4 3 2 1

5 4 3 2 1

 

 

Example: Program to print pyramid of alphabets.

Sam Madam

/* WAP to print a pyramid

A

BA

CBA

DCBA

EDCBA

*/

#include <stdio.h>

#include <conio.h>

void main()

{

for (char f=65;f<=69;f++) // for 5 loops

{

for (char sp=69-f;sp>=65;sp--) //for spaces

{

printf(" ");

}

for (char s=f;s>=65;s--) // to display values

{

printf("%c",s);

}

printf("\n"); // for line break

}

getch();

}

Output

  

A

BA

CBA

DCBA

EDCBA


 

Example: Program to print pyramid of star

Sam Madam

/*Program to print

* * * * *

* * * *

* * *

* *

*

*/

#include <stdio.h>

#include <conio.h>

void main()

{

for (int f=5;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("0 ");

}

printf("\n"); // for line break

}

getch();

}

Output

 

* * * * *

* * * *

* * *

* *

*

 

 

Example: Program to print number triangle

Sam Madam

/* WAP to print a pyramid

5 4 3 2 1

4 3 2 1

3 2 1

2 1

1

 

*/

#include <stdio.h>

#include <conio.h>

void main()

{

for (int f=5;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);

}

 

printf("\n"); // for line break

}

getch();

}

Output

5 4 3 2 1

4 3 2 1

3 2 1

2 1

1

 

 

Example: Program to print number triangle

Sam Madam

/*WAP to print a pyramid

 

9 7 5 3 1

7 5 3 1

5 3 1

3 1 1

*/

#include <stdio.h>

#include <conio.h>

void main()

{

clrscr();

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

{for(int j = 1; j <= i; j++)//for spaces

printf(" ");

for(j = 11-2*i; j >= 1; j -= 2)// to display values

printf("%d ",j);

printf("\n"); // for line break

}

getch();

}

Output

 

9 7 5 3 1

7 5 3 1

5 3 1

3 1

1


Pr.Pg border                                              Next Pg