Skip to main content

Pattern programming

C Programming Code To Create Pyramid and Pattern........

Examples to print half pyramid, pyramid, inverted pyramid, Pascal's Triangle and Floyd's triangle in C Programming using control statements.


List of Source Code
Code to print triangles using *, digits and characters
Code to print inverted triangles using * and digits
Code to print full pyramids
Code to print Pascal's triangle
Code to print Floyd's triangle

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. printf("Enter number of rows: ");
  6. scanf("%d",&rows);
  7. for(i=1; i<=rows; ++i)
  8. {
  9. for(j=1; j<=i; ++j)
  10. {
  11. printf("* ");
  12. }
  13. printf("\n");
  14. }
  15. return 0;
  16. }

Example 2: Program to print half pyramid a using numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, j, rows;
  5. printf("Enter number of rows: ");
  6. scanf("%d",&rows);
  7. for(i=1; i<=rows; ++i)
  8. {
  9. for(j=1; j<=i; ++j)
  10. {
  11. printf("%d ",j);
  12. }
  13. printf("\n");
  14. }
  15. return 0;
  16. }

Example 3: Program to print half pyramid using alphabets

A
B B
C C C
D D D D
E E E E E
Source Code
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, j;
  5. char input, alphabet = 'A';
  6. printf("Enter the uppercase character you want to print in last row: ");
  7. scanf("%c",&input);
  8. for(i=1; i <= (input-'A'+1); ++i)
  9. {
  10. for(j=1;j<=i;++j)
  11. {
  12. printf("%c", alphabet);
  13. }
  14. ++alphabet;
  15. printf("\n");
  16. }
  17. return 0;
  18. }

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. printf("Enter number of rows: ");
  6. scanf("%d",&rows);
  7. for(i=rows; i>=1; --i)
  8. {
  9. for(j=1; j<=i; ++j)
  10. {
  11. printf("* ");
  12. }
  13. printf("\n");
  14. }
  15. return 0;
  16. }

Example 5: Inverted half pyramid using numbers

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1
Source Code
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, j, rows;
  5. printf("Enter number of rows: ");
  6. scanf("%d",&rows);
  7. for(i=rows; i>=1; --i)
  8. {
  9. for(j=1; j<=i; ++j)
  10. {
  11. printf("%d ",j);
  12. }
  13. printf("\n");
  14. }
  15. return 0;
  16. }

 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. printf("Enter number of rows: ");
  6. scanf("%d",&rows);
  7. for(i=1; i<=rows; ++i, k=0)
  8. {
  9. for(space=1; space<=rows-i; ++space)
  10. {
  11. printf(" ");
  12. }
  13. while(k != 2*i-1)
  14. {
  15. printf("* ");
  16. ++k;
  17. }
  18. printf("\n");
  19. }
  20. return 0;
  21. }

Example 7: Program to print pyramid using numbers


        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Source Code
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i, space, rows, k=0, count = 0, count1 = 0;
  5. printf("Enter number of rows: ");
  6. scanf("%d",&rows);
  7. for(i=1; i<=rows; ++i)
  8. {
  9. for(space=1; space <= rows-i; ++space)
  10. {
  11. printf(" ");
  12. ++count;
  13. }
  14. while(k != 2*i-1)
  15. {
  16. if (count <= rows-1)
  17. {
  18. printf("%d ", i+k);
  19. ++count;
  20. }
  21. else
  22. {
  23. ++count1;
  24. printf("%d ", (i+k-2*count1));
  25. }
  26. ++k;
  27. }
  28. count1 = count = k = 0;
  29. printf("\n");
  30. }
  31. return 0;
  32. }

Example 8: Inverted full pyramid using *


* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

Comments

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

C LANGUAGE - OVERVIEW C - VARIABLES

C LANGUAGE - OVERVIEW Advertisements C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute  A N S I A N S ...

environment

It looks like you meant "environment." The environment refers to the surroundings or conditions in which a person, animal, or plant lives or operates. It encompasses various elements, including natural resources, ecosystems, climate, and human-made structures. Protecting and preserving the environment is crucial for the well-being of all living organisms on Earth. Is there anything specific you'd like to discuss or learn about regarding the environment?