Pr.Pg Next Pg

C Pyramid Program Example for Type 1 tutorials

 

Example: Program to print pyramid of * as follow

 Sam Madam

/* WAP to print a pyramid

*

**

***

****

*****

*/

#include <stdio.h>

#include <conio.h>

void main()

{

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

{

for (int s=1;s<=n;s++) // to display values

{

printf("*");

}

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

}

getch();

}

Output

*

**

***

****

*****

 


 

Example: Program to print pyramid of numbers 1, 12, 123, 1234

Sam Madam

/* WAP to print a pyramid

1

12

123

1234

12345

*/

#include<stdio.h>

#include<conio.h>

void main()

{

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

{

for (int s=1;s<=n;s++) // to display values

{

printf("%d",s);

}

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

}

getch();

}

Output

1

12

123

1234

12345

 


 

Example: Program to print a pyramid by entering the no. of lines the user wants

Sam Madam

//WAP to print a pyramid by entering the no. of lines the user wants

#include <stdio.h>

#include <conio.h>

void main()

{int n;

clrscr();

printf("How many lines : ");

scanf("%d",&n);

for(int i = 1; i <= n; i++) // for number of loops

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

printf("%d",i);

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

}

getch();

}

Output

How many lines: 5

1

22

333

4444

55555

 

 

Example: Program to pyramid of 1, 2 3 , 4 5 6 .

Sam Madam

/* WAP to print a pyramid

1

2 3

4 5 6

7 8 9 10

*/

#include <stdio.h>

#include <conio.h>

void main()

{

int s=1;

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

{

for (int j=1;j<=n;j++) // to display values

{

printf("%d",s);

s++;

}

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

}

getch();

}

 

Output

1

2 3

4 5 6

7 8 9 10

 


 

Example: Program to print pyramid of numbers as follows

Sam Madam

/* WAP to print a pyramid

5

54

543

5432

54321

*/

#include <stdio.h>

#include <conio.h>

void main()

{

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

{

for (int j=5;j>=i;j--) // to display values

{

printf("%d",j);

}

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

}

getch();

}

Output

5

54

543

5432

54321

 

 

Example: Program to print reverse pyramid as follows

Sam Madam

/* WAP to print a pyramid

12345

1234

123

12

1

*/

#include <stdio.h>

#include <conio.h>

void main()

{

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

{

for (int s=1;s<=(6-n);s++) //to display values

{

printf("%d",s);

}

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

}

getch();

}

Output

12345

1234

123

12

1

 

 

Example: Program to print reverse pyramid using another for loop method

Sam Madam

/* WAP to print a pyramid

12345

1234

123

12

1

*/

#include <stdio.h>

#include <conio.h>

void main()

{

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

{

for (int s=1;s<=f;s++) // to display values

{

printf("%d",s);

}

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

}

getch();

}

 

Output

12345

1234

123

12

1

 

 

Example: Program to print reverse pyramid of number

Sam Madam

/* WAP to print a pyramid

54321

5432

543

54

5

*/

#include <stdio.h>

#include <conio.h>

void main()

{

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

{

for (int j=5;j>=i;j--) // to display values

{

printf("%d",j);

}

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

}

getch();

}

Output

54321

5432

543

54

5

 

 

Example: Program to print pyramid of star

Sam Madam

/* WAP to print a pyramid

*****

****

***

**

*

*/

#include <stdio.h>

#include <conio.h>

void main()

{

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

{

for (int s=1;s<=(6-n);s++) //to display values

{

printf("*");

}

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

}

getch();

}

Output

*****

****

***

**

*

Pr.Pg border                                              Next Pg