Lab Manual For FPL
Lab Manual For FPL
LAB MANUAL
Experiment/Program 2
To accept from user the number of Fibonacci numbers to be generated and print the
Fibonacci series.
Objective:
To write a C program that accepts a number from the user, indicating how many Fibonacci num-
bers to generate, and then prints the Fibonacci series. The program will be written without using
functions.
Theory:
The Fibonacci series is a sequence of numbers where each number is the sum of the two preced-
ing ones, starting from 0 and 1. The sequence typically looks like this: 0, 1, 1, 2, 3, 5, 8, 13,
21, ...
In mathematical terms:
Fibonacci(0) = 0
Fibonacci(1) = 1
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) for n > 1
Steps Involved:
1. Initialize Variables:
o Start by initializing the first two Fibonacci numbers, which are usually 0 and 1.
o Define a loop control variable to track the number of Fibonacci numbers gener-
ated.
2. Accept User Input:
C Program:
#include <stdio.h>
int main() {
int n, i;
int first = 0, second = 1, next;
1. Initialization:
o first is initialized to 0, representing the first Fibonacci number.
o second is initialized to 1, representing the second Fibonacci number.
o next is used to store the next Fibonacci number in the series.
2. User Input:
o The user is prompted to enter the number of Fibonacci numbers to generate. This
value is stored in the variable n.
3. Generating the Series:
o The program uses a for loop that runs n times to generate the Fibonacci series.
o The first two numbers (0 and 1) are printed directly.
o The next Fibonacci number is calculated by adding first and second.
o The values of first and second are then updated to continue the series.
4. Output:
Observations:
The program correctly generates and prints the first n numbers in the Fibonacci series
based on the user’s input.
If the user enters a non-positive integer, the program prompts them to enter a valid num-
ber.
Conclusion:
This experiment demonstrates how to accept user input, use loops to generate a sequence, and
manage variables in a C program without using functions. The Fibonacci series is a classic exam-
ple of using loops and arithmetic operations in programming.