Tuesday, July 23, 2024
HomeC ProgrammingFibonacci sequence in C programming

# Fibonacci sequence in C programming

## Fibonacci sequence in C programming

Fibonacci
sequence in c programming utilizing loops and recursion. You possibly can print as many phrases
of sequence as your required. The variety of the sequence is called Fibonacci numbers
in c programming. The sequence begin as 0,1,1,2,3,5,8….,. Apart from the primary
two phrases of the sequence each different phrases is sum of earlier two phrases for
instance 5=3+2(addition of three and a couple of).

### Fibonacci sequence Program instance in C

#embrace <stdio.h>

int primary()
{

int n, first = 0, second = 1, subsequent, a;
printf(“Enter the variety of termsn”);
scanf(“%d”, &n);
printf(“First %d phrases of Fibonacci sequence are:n”, n);
for (a = 0; a < n; a++)
{
if (a <= 1)
subsequent = a;
else
{
subsequent = first + second;
first = second;
second = subsequent;

}
printf(“%dn”, subsequent);
}
return 0;
}

### Fibonacci sequence C programming program utilizing recursion

#embrace<stdio.h>

int f(int);

int primary()
{
int n, i = 0, a;
scanf(“%d”, &n);
printf(“Fibonacci sequence phrases are:n”);
for (a = 1; a <= n; a++)
{
printf(“%dn”, f(i));
i++;
}

return 0;
}
int f(int n)
n == 1)
return n;
else
return (f(n-1) + f(n-2));

### Fibonacci sequence C programming program utilizing recursion

The recursive methodology is much less environment friendly because it entails
reoeated operate name whereas calculating bigger time period of the sequence it could lead
to stack overflow. We are able to scale back the operating time of the recursive algorithm by
utilizing memorization( storing Fibonacci numbers which are calculated in an array
and utilizing array for lookup). The Fibonacci sequence has many utility in arithmetic
and Laptop and software program engineering.

RELATED ARTICLES