C code for different types problem


Example 1: Program to print half pyramid using *

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

Source Code

  1. #include
  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(" ");
  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
  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(" ");
  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
  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(" ");
  16. }
  17. return 0;
  18. }

Programs to print inverted half pyramid using * and numbers


Example 4: Inverted half pyramid using *

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

Source Code

  1. #include
  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(" ");
  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
  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(" ");
  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
  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(" ");
  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
  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(" ");
  30. }
  31. return 0;
  32. }

Example 8: Inverted full pyramid using *


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

Source Code

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

Example 9: Print Pascal's triangle


           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1 

Source Code

  1. #include
  2. int main()
  3. {
  4. int rows, coef = 1, space, i, j;
  5. printf("Enter number of rows: ");
  6. scanf("%d",&rows);
  7. for(i=0; i<rows; i++)
  8. {
  9. for(space=1; space <= rows-i; space++)
  10. printf(" ");
  11. for(j=0; j <= i; j++)
  12. {
  13. if (j==0 || i==0)
  14. coef = 1;
  15. else
  16. coef = coef*(i-j+1)/j;
  17. printf("%4d", coef);
  18. }
  19. printf(" ");
  20. }
  21. return 0;
  22. }

Example 10: Print Floyd's Triangle.

1
2 3
4 5 6
7 8 9 10

Source Code

  1. #include
  2. int main()
  3. {
  4. int rows, i, j, number= 1;
  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 ", number);
  12. ++number;
  13. }
  14. printf(" ");
  15. }
  16. return 0;
  17. }

14 comments:

C code for if &amp; if else statement with diagram

If statement in C programming with example BY er RAVI BHADANA  |  FILED UNDER:  C-PROGRAMMING When we need to execute a block of statements ...