Wednesday, 8 December 2010

program To print the Fibonacci series for 1 to n value


#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=2;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
  1. Enter n value : 7
0     1 1 2 3 5 8
4. Enter n value : -6
0     1

Conclusion : The program is error free



7

VIVA QUESTIONS:

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