Saturday, September 17

C program to print a Number pattern

Write a program to print the following pattern:
0

1 1
2 3 5
8 13 21 34



Hi buddy, Jst analyse the no.s in order, 0,1,1,2,3,5,8,13,21,34....
Yess!!!! this is a Fibonacci series.

0+1=1
1+1=2
1+2=3
2+3=5... and so on

Next requirement is to generate pattern. First line has 1 element, second line has 2 elements and so on. And total number of elements in whole pattern will be N*(N+1)/2 , if there are N number of lines.





#include<stdio.h>
main()
{
      int n,i,j,k=0;
      int a[20];
      printf("Enter the lmit..:   ");
      scanf("%d",&n);
      a[0]=0;
      a[1]=1;
      for(i=2;(i<n*(n+1)/2);i++)
      a[i]=a[i-1]+a[i-2];
      for(j=0;j<=n;j++)
     {
      for(i=0;i<j;i++)
      printf("%d\t",a[k+i]);
      k+=i;
      printf("\n");
      }
      getch();
}

1 comments :

Nimish Garg said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Check this out
http://cbasicprogram.blogspot.in/2012/04/number-patterns.html

Post a Comment