Code 1:Fibonacci series in c using while loop
____________________________________________________________
Code 3: Fibonacci series in c using for loop
Code 4: Sum of Fibonacci series in c
#include<stdio.h>
int main(){
int k=2,n;
long int i=0,l,j=1,f;
printf("Enter the number range:");
scanf("%d",&n);
printf("Fibonacci series is: %ld %ld",i,j);
while(k<n){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
k++;
}
return 0;
}
output:
Enter the number range: 9
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21
____________________________________________________________
Code 2: Fibonacci series using array in c
#include<stdio.h>
int main(){
int i,n;
long int arr[40];
printf("Enter the number range: ");
scanf("%d",&n);
arr[0]=0;
arr[1]=1;
for(i=2;i<n;i++){
arr[i] = arr[i-1] + arr[i-2];
}
printf("Fibonacci series is: ");
for(i=0;i<n;i++)
printf("%ld ",arr[i]);
return 0;
}
output:
Enter the number range: 9
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 Code 3: Fibonacci series in c using for loop
#include<stdio.h>
int main(){
int k,n;
long int i=0,l,j=1,f;
printf("Enter the number range:");
scanf("%d",&n);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j);
for(k=2;k<n;k++)
{
{
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
return 0;
}
output:
Enter the number range: 9
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21
____________________________________________________________
Code 4:
#include<stdio.h>
int main(){
int k,n;
long int i=0,j=1,f;
long int sum = 1;
printf("Enter the number range: ");
scanf("%d",&n);
for(k=2;k<n;k++){
f=i+j;
i=j;
j=f;
sum = sum + j;
}
printf("Sum of Fibonacci series is: %ld",sum);
return 0;
}
output:
Enter the number range:5
FIBONACCI SERIES: 7
No comments:
Post a Comment