0% found this document useful (0 votes)
28 views3 pages

Lab Manual For FPL

The document is a lab manual for an experiment in a first-year engineering course at ISBM College, focusing on generating Fibonacci numbers using a C program. It outlines the objective, theory, steps involved, and provides a complete C program to generate and print the Fibonacci series based on user input. The conclusion emphasizes the educational value of the experiment in understanding loops and arithmetic operations in programming.

Uploaded by

kurhadeanjali54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Lab Manual For FPL

The document is a lab manual for an experiment in a first-year engineering course at ISBM College, focusing on generating Fibonacci numbers using a C program. It outlines the objective, theory, steps involved, and provides a complete C program to generate and print the Fibonacci series based on user input. The conclusion emphasizes the educational value of the experiment in understanding loops and arithmetic operations in programming.

Uploaded by

kurhadeanjali54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Peoples Empowerment Group

ISBM COLLEGE OF ENGINEERING, NANDE, PUNE


DEPARTMENT OF FIRST YEAR ENGINEERING
Academic Year 2024-25

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:

oPrompt the user to enter the number of Fibonacci numbers to be generated.


oStore this value in an integer variable.
3. Generate Fibonacci Series:

oUse a loop to generate the Fibonacci series.


oIn each iteration, compute the next Fibonacci number by summing the last two
numbers.
o Update the values of the two preceding numbers for the next iteration.
o Print the Fibonacci number in each loop iteration.
4. Terminate the Program:
Peoples Empowerment Group
ISBM COLLEGE OF ENGINEERING, NANDE, PUNE
DEPARTMENT OF FIRST YEAR ENGINEERING
Academic Year 2024-25
o End the loop after the required number of Fibonacci numbers is generated.
o Print a message indicating the end of the series.

C Program:

#include <stdio.h>

int main() {
int n, i;
int first = 0, second = 1, next;

// Step 1: Accept user input


printf("Enter the number of Fibonacci numbers to generate: ");
scanf("%d", &n);

// Step 2: Handle special cases for n <= 0


if (n <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}

// Step 3: Print the Fibonacci series


printf("Fibonacci Series: ");

for (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"); // New line at the end


return 0;
Peoples Empowerment Group
ISBM COLLEGE OF ENGINEERING, NANDE, PUNE
DEPARTMENT OF FIRST YEAR ENGINEERING
Academic Year 2024-25
}

Explanation of the Program:

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:

o The Fibonacci series is printed on the console.


o A newline character (\n) is printed at the end to ensure the output is cleanly
formatted.

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.

You might also like