Q.) WAP to print Fibonacci-series up to that number using while loop in C program.
#include < stdio.h>
int main ()
{
int i, n, j, k;
printf ("Enter a Number : ");
scanf ("%d", &n);
i = 0;
j = 1;
printf ("%d %d ", i, j);
k = i + j;
while (k <= n)
{
printf (" %d", k);
i = j;
j = k;
k = i + j;
}
getch();
}
Output
Enter a Number : 10
0 1 1 2 3 5 8
Program Explanation
Step 1 : Include header files (#include< stdio.h> and #include< conio.h>).
Step 2 : Start with main function with return type.
Step 3 : parenthesis to start and end the program { }.
Step 4 : declare variables with data type i.e, 'n' is an integer type so we use "int" data type.
Step 5 : Use output function printf() to print the output on the screen.
Step 6 : Use input function scanf() to get input from the user.
Step 7 : here, we have print Fibonacci-series up to that number using while loop, enter a number : , we can enter 10 , then check the condition (k <= n) and condition was true the , print Fibonacci-serieso of a number 0 1 1 2 3 5 8 .
Step 8 : using getch() function to hold the screen.