You are on page 1of 12

Programs to print triangles using *, numbers and characters

Example 1: Program to print half pyramid using *

**

***

****

*****

Source Code

1. #include <stdio.h>

2. int main()

3. {

4. int i, j, rows;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.

9. for(i=1; i<=rows; ++i)

10. {

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

12. {

13. printf("* ");

14. }

15. printf("\n");

16. }

17. return 0;

18. }
Example 2: Program to print half pyramid a using numbers

12

123

1234

12345

Source Code

1. #include <stdio.h>

2. int main()

3. {

4. int i, j, rows;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.

9. for(i=1; i<=rows; ++i)

10. {

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

12. {

13. printf("%d ",j);

14. }

15. printf("\n");

16. }

17. return 0;

18. }
Example 3: Program to print half pyramid using alphabets

BB

CCC

DDDD

EEEEE

Source Code

1. #include <stdio.h>

2. int main()

3. {

4. int i, j;

5. char input, alphabet = 'A';

6.

7. printf("Enter the uppercase character you want to print in last row: ");

8. scanf("%c",&input);

9.

10. for(i=1; i <= (input-'A'+1); ++i)

11. {

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

13. {

14. printf("%c", alphabet);

15. }

16. ++alphabet;

17.

18. printf("\n");

19. }

20. return 0;
21. }

Programs to print inverted half pyramid using * and numbers

Example 4: Inverted half pyramid using *

*****

****

***

**

Source Code

1. #include <stdio.h>

2. int main()

3. {

4. int i, j, rows;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.

9. for(i=rows; i>=1; --i)

10. {

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

12. {

13. printf("* ");

14. }

15. printf("\n");

16. }

17.
18. return 0;

19. }

Example 5: Inverted half pyramid using numbers

12345

1234

123

12

Source Code

1. #include <stdio.h>

2. int main()

3. {

4. int i, j, rows;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.

9. for(i=rows; i>=1; --i)

10. {

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

12. {

13. printf("%d ",j);

14. }

15. printf("\n");

16. }

17.
18. return 0;

19. }

 Programs to display pyramid and inverted pyramid using * and digits

Example 6: Program to print full pyramid using *

***

*****

*******

*********

Source Code

1. #include <stdio.h>

2. int main()

3. {

4. int i, space, rows, k=0;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.

9. for(i=1; i<=rows; ++i, k=0)

10. {

11. for(space=1; space<=rows-i; ++space)

12. {

13. printf(" ");

14. }

15.

16. while(k != 2*i-1)


17. {

18. printf("* ");

19. ++k;

20. }

21.

22. printf("\n");

23. }

24.

25. return 0;

26. }

Example 7: Program to print pyramid using numbers

232

34543

4567654

567898765

Source Code

1. #include <stdio.h>

2. int main()

3. {

4. int i, space, rows, k=0, count = 0, count1 = 0;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.
9. for(i=1; i<=rows; ++i)

10. {

11. for(space=1; space <= rows-i; ++space)

12. {

13. printf(" ");

14. ++count;

15. }

16.

17. while(k != 2*i-1)

18. {

19. if (count <= rows-1)

20. {

21. printf("%d ", i+k);

22. ++count;

23. }

24. else

25. {

26. ++count1;

27. printf("%d ", (i+k-2*count1));

28. }

29. ++k;

30. }

31. count1 = count = k = 0;

32.

33. printf("\n");

34. }

35. return 0;
36. }

Example 8: Inverted full pyramid using *

*********

*******

*****

***

Source Code

1. #include<stdio.h>

2. int main()

3. {

4. int rows, i, j, space;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.

9. for(i=rows; i>=1; --i)

10. {

11. for(space=0; space < rows-i; ++space)

12. printf(" ");

13.

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

15. printf("* ");

16.

17. for(j=0; j < i-1; ++j)


18. printf("* ");

19.

20. printf("\n");

21. }

22.

23. return 0;

24. }

Example 9: Print Pascal's triangle

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

Source Code

1. #include <stdio.h>

2. int main()

3. {

4. int rows, coef = 1, space, i, j;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.

9. for(i=0; i<rows; i++)

10. {
11. for(space=1; space <= rows-i; space++)

12. printf(" ");

13.

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

15. {

16. if (j==0 || i==0)

17. coef = 1;

18. else

19. coef = coef*(i-j+1)/j;

20.

21. printf("%4d", coef);

22. }

23. printf("\n");

24. }

25.

26. return 0;

27. }

Example 10: Print Floyd's Triangle.

23

456

7 8 9 10

Source Code

1. #include <stdio.h>

2. int main()

3. {
4. int rows, i, j, number= 1;

5.

6. printf("Enter number of rows: ");

7. scanf("%d",&rows);

8.

9. for(i=1; i <= rows; i++)

10. {

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

12. {

13. printf("%d ", number);

14. ++number;

15. }

16.

17. printf("\n");

18. }

19.

20. return 0;

21. }

You might also like