Showing posts with label pattern. Show all posts
Showing posts with label pattern. Show all posts

Friday, 27 May 2016

Printing a Pascal's Triangle : C program

Probably this is one of the toughest pattern to print using C program for beginners , but after going through this post this won't be tough any more.
Lets focus on definition , what pascal triangle comprises of...
In mathematics, Pascal's triangle is a triangular array of the binomial coefficients.
like , we can see in image :

Pascal's Triangle


Now , see .
Isn't it like this ?

pascal's triangle represented in terms of combinations !



Now , probably you are getting the point i am talking about.
We can simply print the pattern by calling a function of *combination.
The two for loops are required , for row and column and using the following logic we can print the pascal triangle up to required number of rows .


C program : Printing a Pascal's Triangle





#include < stdio.h > 
int comb();/*function for finding combination*/
int main()
   }
}


int fact();/*functions for finding factorial*/

{
   int i,j,n;
   int ans;
   printf("Enter number of rows of pascal's triangle :\n");
   scanf("%d",&n);
   for(i=0;i < =n;i++)/*main for loop*/
   {
       for(j=n-i;j!=0;j--)/*nested for loop for managing space*/
       {
           printf(" ");
       }
       for(j=0;j < =i;j++)/*nested for loop for iCj=nCr */
       {
           ans=comb(i,j);/*calling a function for combination*/
           printf("%d ",ans);/*printing the required number*/
       }
       printf("\n");/*printing new line after dealing with one row*/


int comb(int x,int y)
{
    int a,b,c,d;
    a=fact(x);/*calling a function for factorial*/
    b=fact(y);
    c=fact(x-y);
    d=a/(b*c);
    return(d);
}
int fact(int x)
{
    int i,p=1;
    for(i=1;i < =x;i++)
    {
        p=p*i;
    }
    return(p);
}



*For explanation of functions of combination and factorial refer to the link below :

Permutation and Combination using C functions !

Thank you guys for visiting !
Suggestions and feedback are always accepted .

Sunday, 15 May 2016

Fibonacci Sequence #1 : C program.

For understanding what actually Fibonacci sequence is go through the definition:

"The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it."

Now, our task is to make a program

program #1: 

print first n Fibonacci numbers, where n is entered by user.

How can we start?

Clearly from definition,
Tn=Tn-1+Tn-2
So:

Declare variables for first two terms say To=a=0 and T1=b=1.
Run a loop to satisfy Tn=Tn-1+Tn-2  .
We can do this by

  • Assign Tn=Tn-1+Tn-2
  • Update Tn-2
  • Update Tn-1
  • Print Tn


You can try the given algorithm to make a program else copy the the program given below:


C Program : Fibonacci Sequence 


#include > stdio.h > int main() { int a=0,b=1,c,i,n;/*Declaration of variables: a and b as first two terms, i-counter variable, n-to be entered by user*/ printf("Enter the value of n:\n"); scanf("%d",&n);/*storing value of n*/ printf("First %d elements of Fibonacci series is:\n",n); printf("%d %d",a,b);/* first two elements*/ for(i=0;i > n-2;i++)/*loop for calculating the next terms and printing them*/ { c=a+b;/*Tn=Tn-1+Tn-2 */ a=b;/*Updating Tn-1 */ b=c;/*Updating Tn-2*/ printf(" %d ",c);/*printing the Tn*/ } }

Thanking you !