Wednesday 12 September 2012

To print the Fibonacci series for 1 to n value


Description
A fibonacci series is defined as follows
The first term in the sequence is 0
The second term in the sequence is 1
The sub sequent terms 1 found by adding the preceding two terms in the sequence
Formula: let t1,t2,…………tn be terms in fibinacci sequence
                        t1=0, t2=1
                        tn=tn-2+tn-1……where n>2
algorithm:
Step 1: start
Step 2: initialize the a=0, b=1
Step 3: read n
Step 4: if n== 1 print a    go to step 7. else goto step 5
Step 5: if n== 2 print a, b go to step 7 else print a,b
Step 6: initialize i=3
i)                    if  i<= n do as follows. If not goto step 7
c=a+b
print c
a=b
b=c
increment I value
goto step 6(i)
Step 7: stop
 
Program:
           
#include<stdio.h>
void main()
{
  int a,b,c,n,i;
  clrscr();
  printf("enter n value");
  scanf("%d",&n);
  a=0;
  b=1;
  if(n==1)
  printf("%d",a);
  else
  if(n==2)
  printf("%d%d",a,b);
  else
   {
             printf("%d%d",a,b);
//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE
               for(i=3;i<=n;i++)
             {
                        c=a+b;
                        printf("%d",c);
                        a=b;
                        b=c;
              }
getch();
     }
}

Output:
1. Enter n value : 5
0 1  1  2  3
2. Enter n value : 7
0        1  1  2  3  5  8
3. Enter n value : -6
0        1
Conclusion : The program is error free





VIVA QUESATIONS:

1)      What is Fibonacci series ?
Ans:   A fibonacci series is defined as follows
The first term in the sequence is 0
The second term in the sequence is 1
The sub sequent terms 1 found by adding the preceding two terms in the sequence
Formula: let t1,t2,…………tn be terms in fibinacci sequence
                        t1=0, t2=1
                        tn=tn-2+tn-1……where n>2
    2)    What are the various types of unconditional statements?
   Ans:     goto,Break and continue

3)What are the various types of conditional statements?
  Ans:    if , if else ,switch statements
4)      Expand <STDIO.H >?
Ans: standard input output header file






No comments:

Post a Comment