The Program in C that Prints the Fibonnicci series1, 1, 2, 3, 5, 8, 13..N is given below:
#include <stdio.h>
int main() {
int n, a = 0, b = 1, c;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("Fibonacci series: ");
for (int i = 1; i <= n; i++) {
if (i == 1) {
printf("%d, ", a);
continue;
}
if (i == 2) {
printf("%d, ", b);
continue;
}
c = a + b;
a = b;
b = c;
printf("%d, ", c);
}
return 0;
}
Output:
Enter the value of N: 45
Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733,
Pro-Tips💡
This program prompts the user to enter an integer N, then uses a for loop to print the first N numbers of the Fibonacci series.
The Fibonacci series is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
The program initializes two variables a
and b
to 0 and 1 respectively, which are the first two numbers of the series.
In the loop, it uses an if statement to check if the current iteration is the first or second iteration, if it is, it prints the initial values of a and b respectively,
otherwise it calculates the next number in the series (c=a+b), updates the value of a and b and prints the value of c. This way the Fibonacci series from 1 to N will be printed.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.