Monday, August 31, 2015

C Programs on flow control statements (if, switch, loop, break and continue)-5

2015 & 2016 Batch Freshers Registration Link

JOIN MY FACEBOOK GROUP FOR UPDATES 

C pattern programs

C program to print following pyramid pattern of stars

                                 
main()
{
          int row, c, n, temp;
          printf("Enter the number of rows in pyramid of stars you wish to see ");
          scanf("%d",&n);

          temp = n;
          for ( row = 1 ; row <= n ; row++ )
          {
                    /* for spacing purpose */
                    for ( c = 1 ; c < temp ; c++ )
                             printf(" ");
      
temp--;
/* for printing stars */
                    for ( c = 1 ; c <= 2*row - 1 ; c++ )
                             printf("*");
                    printf("\n");
          }
 }

Write a c program to print Pascal triangle.

In mathematics, Pascal's triangle is a triangular array of the binomial coefficients. In much of the Western world it is named after French mathematician Blaise Pascal, although other mathematicians studied it centuries before him in India, Iran, China, Germany, and Italy.


long factorial(int);
main()
{
          int row, c, n, temp;
          printf("Enter the number of rows you wish to see in pascal triangle\n");
          scanf("%d",&n);

          temp = n;
          for ( row = 0 ; row < n ; row++ )
          {
                    /* for spacing purpose */
                    for ( c = 1 ; c < temp ; c++ )
                             printf(" ");
      
temp--;
/* for printing stars */
                    for ( c = 0 ; c <= row ; c++ )
                             printf("%ld ",factorial(row)/(factorial(c)*factorial(row-c)));
                    printf("\n");
          }
 }

long factorial(int n)
{
          int c;
          long int result = 1;
          for (c = 1; c <= n; c++)
                   result = result*c;
          return result;
}

Write a c program for Floyd’s triangle.
This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.

main()
{
int n, row,  c, a = 1;

          printf("Enter the number of rows of Floyd's triangle to print\n");
          scanf("%d", &n);

          for (row = 1; row <= n; row++)
          {
                    for (c = 1; c <= row; c++)
                    {
                              printf("%d ",a);
                               a++;
                    }
                    printf("\n");
          }

 }