Write a program to print Fibonacci series without using recursion?

#include
#include
void main()
{
int n1=0,n2=1,n3,i,number;
clrscr();
printf(“Enter the number of elements:”);
scanf(“%d”,&number);
printf(“\n%d %d”,n1,n2);//printing 0 and 1

for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(” %d”,n3);
n1=n2;
n2=n3;
}
getch();
}

Certainly! You can achieve the Fibonacci series without using recursion by using a simple iterative approach. Here’s a C program to print the Fibonacci series without recursion:

c
#include <stdio.h>

void fibonacci(int n) {
int a = 0, b = 1, c, i;

if (n <= 0)
return;

printf("%d %d ", a, b); // Print first two Fibonacci numbers

for (i = 2; i < n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}

int main() {
int n;
printf("Enter the number of terms for Fibonacci series: ");
scanf("%d", &n);

printf("Fibonacci Series: ");
fibonacci(n);

return 0;
}

This program takes an input n from the user and prints the first n terms of the Fibonacci series iteratively without using recursion. It starts with the first two terms (0 and 1) and then iteratively calculates the next term by adding the previous two terms. Finally, it prints each term until it reaches the specified number of terms n.