Patterns in C


Patterns in C 

The given programs print various different patterns of numbers and stars.Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.



   *
   ***
   *****
   *******
   ********
 
We have shown five rows above in the program. 
If you want more/less rows then you will be asked to enter the numbers of rows ,
you want to print in the pyramid of stars. 
 
 
#include <stdio.h>
int 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 ( c = 1 ; c < temp ; c++ )
         printf(" ");  // for space
       temp--;
       for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*"); //for printing the *
 
      printf("\n");
   }
    return 0;
}
 
 

Pascal Triangle in C:



   1
   1 1
   1 2 1
   1 3 3 1
 

Program for Pascal Triangle :

 
#include <stdio.h>
long factorial(int);
int main()
{
   int i, n, c;
   printf("Enter the number of rows: \n");
   scanf("%d",&n);
   for (i = 0; i < n; i++)
   {
      for (c = 0; c <= (n - i - 2); c++)
         printf(" ");
 
      for (c = 0 ; c <= i; c++)
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
 
      printf("\n");
   }
   return 0;
}
 
long factorial(int n)
{
   int c;
   long result = 1;
   for (c = 1; c <= n; c++)
         result = result*c;
   return result;
}
 
 

Floyd's triangle in c :

 
1
2 3
4 5 6
7 8 9 10
 

Program for Floyd's Triangle :

 
#include <stdio.h>
int main()
{
  int n, i,  c, a = 1;
  printf("Enter the Number of Rows: \n");
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }
  return 0;
}
 
Thank you for visiting. :)
Happy Coding... ;) :D
 

No comments:

Powered by Blogger.