C
O
D
E
IN C PROGRAM
CODE:
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next;
printf(“Enter the number of terms: “);
scanf(“%d”, &n);
printf(“Fibonacci Series: “);
for (int i = 1; i <= n; i++) {
if (i == 1) {
printf(“%d, “, first);
continue;
}
if (i == 2) {
printf(“%d, “, second);
continue;
}
next = first + second;
first = second;
second = next;
printf(“%d, “, next);
}
printf(“\n”);
return 0;
}
Table of Contents
P
R
O
G
R
A
M
I
N
G
E X E C U T E
Execution of fibonacci series in c program
E X P L A N A T I O N
IN C PROGRAM
Overview
The provided C code is a program that generates and prints a Fibonacci series based on user input. It uses a for
loop to calculate and display the Fibonacci sequence. Let’s break down the code into its components and explain each part.
Code Structure
The code consists of several key parts:
- Header File Inclusion
Overview
The provided C code is a program that generates and prints a Fibonacci series based on user input. It uses a for
loop to calculate and display the Fibonacci sequence. Let’s break down the code into its components and explain each part.
Code Structure
The code consists of several key parts:
-
Header File Inclusion#include <stdio.h>
- Main Function
int main() {
// Code
return 0;
}
The
main
function is the entry point of the program, where the program execution begins. It returns an integer value (0 in this case) to indicate the success of the program. - Variable Declarations
int n, first = 0, second = 1, next;
Here, three integer variables are declared:
n
,first
,second
, andnext
. These variables are used to store user input and intermediate values during the Fibonacci series generation. - User Input
printf(“Enter the number of terms: “);
scanf(“%d”, &n);The program prompts the user to enter the number of terms they want in the Fibonacci series. The input is read using
scanf
and stored in the variablen
.- Print Statement
printf(“Fibonacci Series: “);
This line is a print statement that informs the user that the following numbers will be the Fibonacci series.
- For Loop for Fibonacci Calculation and Printing
for (int i = 0; i < n; i++){
// Code
}This is the heart of the program. It uses a
for
loop to generate and print the Fibonacci series. The loop iterates fromi = 0
toi < n
, wheren
is the user-provided number of terms.
- Print Statement
-
Fibonacci Calculation
Now, let’s examine the logic inside the
for
loop to understand how the Fibonacci series is generated and printed.if (i <= 1)
next = i;
else{
next = first + second;
first = second;
second = next;
}
printf(“%d “, next);
The code within the
for
loop calculates and prints the next term in the Fibonacci series. It does this using anif-else
statement to handle the initial cases (i.e., the first two terms) and the subsequent terms differently.- Initial Cases (i <= 1):
- When
i
is 0 or 1 (the first two terms of the Fibonacci sequence),next
is set to the value ofi
. In this case,first
andsecond
remain unchanged.
- When
- Subsequent Terms (i > 1):
- For terms beyond the first two,
next
is calculated as the sum offirst
andsecond
, which represent the previous two terms. After the calculation, the values offirst
andsecond
are updated.first
takes the value ofsecond
, andsecond
takes the value ofnext
.
- For terms beyond the first two,
Finally, the value of
next
is printed, followed by a space to separate it from the next term. This process repeats for each term in the series until the loop terminates.Output
The generated Fibonacci terms are printed on the screen as they are calculated. The loop iterates
n
times, so the user gets the requested number of terms in the Fibonacci series.Program Execution
Here’s a step-by-step breakdown of how the program works:
- The program starts by including the necessary header file and defining the
main
function. - It declares four integer variables:
n
(for user input) andfirst
,second
, andnext
for Fibonacci series calculations. - The user is prompted to enter the number of terms they want in the Fibonacci series.
- The user’s input is read and stored in the variable
n
. - The program prints a message indicating that it’s about to display the Fibonacci series.
- The
for
loop starts to generate and print the series.a. Ifi
is 0 or 1, the value ofnext
is set toi
.b. For all other values ofi
,next
is calculated as the sum offirst
andsecond
. The variablesfirst
andsecond
are updated accordingly.c. The value ofnext
is printed, followed by a space. - The loop continues to run until
i
reachesn - 1
, ensuring the requested number of terms is generated. - The program finishes execution, and the final Fibonacci series is displayed on the screen.
Fibonacci Series Generation
The Fibonacci series is generated as follows:
- The first two terms are fixed at 0 and 1.
- Subsequent terms are calculated by adding the previous two terms.
- For each iteration, the program prints the
next
term, which is the sum of thefirst
andsecond
terms. first
andsecond
are updated accordingly for the next iteration.
Example Output
Let’s say the user enters
10
as the number of terms. The program will generate and print the first 10 terms of the Fibonacci series, which will look like this:mathematicalEnter the number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
Each term is separated by a space, and the series ends after displaying the 10th term.
Conclusion
This C program demonstrates how to generate and print a Fibonacci series using a
for
loop. It takes user input to determine the number of terms to generate and employs a straightforward logic for calculating each term in the series. The output is presented in a user-friendly format, making it easy to visualize the Fibonacci sequence based on the user’s input. - Initial Cases (i <= 1):
MORE PROGRAM :
- https://d-techsol.in/in-c-programming-write-a-code-of-lcm-of-two-number/
- https://d-techsol.in/write-a-program-of-addition-of-number-in-c/
BEST C PROGRAMMING BOOK :