Pr.Pg Next Pg

C pyramid programs on Type 3 tutorials

 

Example: Program to print pyramid of stars

 Sam Madam

/*WAP to print a pyramid

*

* * *

* * * * *

* * * * * * *

* * * * * * * * *

*/

#include <stdio.h>

#include <conio.h>

void main()

{

clrscr();

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

{

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

{

printf(" ");

}

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

{

printf("* ");

}

for (int x=2;x<=f;x++) //for printing values

{

printf("* ");

}

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

}

getch();

}

Output

 

*

* * *

* * * * *

* * * * * * *

* * * * * * * * *

 

 

Example: Program to print pyramid of alphabets.

Sam Madam

/*WAP to print a pyramid

A

B B

C C C

D D D D

*/

#include <stdio.h>

#include <conio.h>

 

void main()

{

int i, j;

clrscr();

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

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

printf(" ");

for(j = 1; j <= i; j++) //for printing values

printf("%c ",(i+64));

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

}

getch();

}

Output

 

A

B B

C C C

D D D D

 

 

Example: Program to print pyramid of alphabets

Sam Madam

/*WAP to print a pyramid

A

ABA

ABCBA

ABCDCBA

*/

#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

{

clrscr();

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

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

printf(" ");

for(j = 1; j <= 2*i-1; j++)

if(j <= i)

printf("%c",(j+64)); //for printing values

else

printf("%c",(2*i-j+64));

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

}

getch();

}

Output

A

ABA

ABCBA

ABCDCBA

 


 

Example: Program to print pyramid 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

*/

#include <stdio.h>

#include <conio.h>

void main()

{

clrscr();

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--) //for printing values

{

printf("%d ",s);

}

for (int x=2;x<=f;x++) //for printing values

{

printf("%d ",x);

}

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

}

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

 


 

Example: Program to print pyramid of numbers

Sam Madam

/*WAP to print a pyramid

1

232

34543

4567654

567898765

*/

#include <stdio.h>

#include <conio.h>

void main()

{

int i,s,f,x;

for (i=0;i<6;i++) //for 5 loops

{

for (s=0;s<=6-i;s++)//for spaces

{

printf(" ");

}

 

// for first value

for (f=0;f<i;f++)

printf("%d",i+f); //for printing values

 

// for second value

for (x=f-2;x>=0;x--)

printf("%d",x+i); //for printing values

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

}

getch();

}

Output

 

1

232

34543

4567654

567898765

 

 

Example: Program to print pyramid of numbers.

Sam Madam

/*WAP to print a pyramid

1

121

12321

1234321

123454321

*/

 

#include <stdio.h>

#include <conio.h>

void main()

{

int i,s,j,k;

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

{

for (s=1;s<=5-i;s++)//for spaces

{

printf(" ");

}

 

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

{

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

}

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

{

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

}

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

}

getch();

}

Output

 

1

121

12321

1234321

123454321

 

 

Example: Program to print pyramid of numbers

Sam Madam

/*WAP to print a pyramid*/

	   0
	 1 0 1
   2 1 0 1 2
 3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4
*/

#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

{

clrscr();

for(int i = 0; i <= 4; i++) //for 5 loops

{

for(int j = 1; j <= 15-i; j++)

printf(" ");//for spaces

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

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

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

}

getch();

}

Output

 
	   0
	 1 0 1
   2 1 0 1 2
 3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4

 


 

Example: Program to print pyramid of stars

Sam Madam

/*WAP to print a pyramid

*******

*****

***

*

*/

#include<stdio.h>

void main()

{

int i,j,k;

for(i=0;i<4;i++) //for 4 loops

{

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

{

printf(" ");//for spaces

}

for(k=2*(4-i);k>1 ;k--)

{

printf("*"); //for printing values

}

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

}

}

Output

*******

*****

***

*

 

 

Example: Program to print pyramid of alphabets

Sam Madam

/*WAP to print a pyramid

A B C D C B A

A B C B A

A B A

A

*/

#include <stdio.h>

#include <conio.h>

#include <math.h>

 

void main()

{

clrscr();

for(int i = 4; i >= 1; i--) //for 4 loops

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

printf(" ");

for(j = 1; j <= 2*i-1; j++)

if(j <= i)

printf("%c ",j+64); //for printing values

else

printf("%c ",2*i-j+64); //for printing values

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

}

getch();

}

Output

A B C D C B A

A B C B A

A B A

A

 

 

Example: Program to print pyramid of alphabets

Sam Madam

/*WAP to print a pyramid

FEDCBABCDEF

EDCBABCDE

DCBABCD

CBABC

BAB

A

*/

#include <stdio.h>

#include <conio.h>

void main()

{

for (int f=70;f>=65;f--) //to print 6 loops

{

for (int sp=(70-f);sp>=0;sp--)

{

printf(" "); //to print spaces

}

for (char s=f;s>=65;s--)

{

printf("%c",s); //to print values

}

for (char x=66;x<=f;x++)

{

printf("%c",x); //to print values

}

printf("\n"); //to print line break

}

getch();

}

Output

 

FEDCBABCDEF

EDCBABCDE

DCBABCD

CBABC

BAB

A

 


 

Example: Program to print pyramid of numbers

Sam Madam

/*WAP to print a pyramid

3 2 1 0 1 2 3
2 1 0 1 2
1 0 1 0
*/

#include <stdio.h>

#include <conio.h>

#include <math.h>

 

void main()

{

clrscr();

for(int i = 3; i >=0; i--) //for 4 loops

{for(int j = 1; j <= 15-i; j++)

printf(" "); //for spaces

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

printf("%d ",abs(j)); //to print values

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

}

getch();

}

Output

 

3 2 1 0 1 2 3
2 1 0 1 2
1 0 1
0

Pr.Pg border                                              Next Pg