Tuesday 29 August 2017

C program to print the numbers between 1 to n that do not appear in Fibonacci series


Problem:
Write a C program to print the numbers between 1 to n that do not appear in Fibonacci series.

Program:
#include<stdio.h>
void main()
{
int n,i,f0=0,f1=1,f2=f0+f1;
printf("enter the number of terms");
scanf("%d",&n);
while(f2<=n)
                {
                                for(i=f1+1;i<f2;i++) /*print all numbers between f1 and f2 */
                                 {
                                                printf("%d\t",i);
                                 }
                                 f0=f1;
                                 f1=f2;
                                 f2=f0+f1;
 }

}

Output:




(or)

#include <stdio.h>
main()
{
               int i,n,f0=0,f1=1,f2=f0+f1;
               printf("enter the number of terms");
               scanf("%d",&n);
               for(i=1;i<=n;i++)
               {
                              if(i != f2)
                                             printf("%d\t",i);
                              if(i==f2) /* we generate next fibonacci term if all the numbers till  f2 are printed */
                              {
                                             f0=f1;
                                             f1=f2;
                                             f2=f0+f1;
                              }
               }
}

No comments:

Post a Comment