Vous êtes sur la page 1sur 5

Lab No: 01

Lab Title: “Factorial & Fibonacci Series”.

Objective:
❖ Learn about the program of finding Factorial of any number.
❖ Learn about the program of Fibonacci Series.

Description:

Factorial:
Factorial is the product of all positive integers from 1 to a given number "n." For example, 5! = 5
x 4 x 3 x 2 x 1 = 120.

Algorithm of factorial of a number:

Without recursion :

1. initialize i=1 and n=1


2. input number num
3. while(i<=num) do
4. n=n*i
5. i++
6. end of while
7. print n //factorial of num is stored in n
With recursion :

//Factorial function ‘int fact(int num)’ where ‘num’ is the user given number

1. initialise f=1
2. if(num==1) then
3. return num
4. end of if
5. f=num*fact(num-1)
6. return f

Flow chart:
Source Code(Without Recursion):

#include <stdio.h>

int main()
{
int c, n, f = 1;

printf("Enter a number to calculate its factorial\n");


scanf("%d", &n);
for (c = 1; c <= n; c++)
f = f * c;

printf("Factorial of %d = %d\n", n, f);

return 0;
}

Using Recursion:

#include<stdio.h>

long factorial(int);

int main()
{
int n;
long f;

printf("Enter an integer to find its factorial\n");


scanf("%d", &n);

if (n < 0)
printf("Factorial of negative integers isn't defined.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}

return 0;
}
long factorial(int n)
{
if (n == 0) // Base case
return 1;
else
return (n*factorial(n-1));
}

Output:
Enter a number to calculate its factorial
11
Factorial of 11 = 39916800

Fibonacci Series:
The Fibonacci series is a sequence where each number is the sum of the two preceding ones. It
starts with 0 and 1, and the series continues as 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.

Algorithm:
Step-1 Start
Step-2 Input Value of N
Step-3 A=0, B=1, COUNT=2
Step-4 WRITE A, B
Step-5 IF (COUNT >N) then go to step 12
Step-6 NEXT= A + B
Step-7 WRITE NEXT
Step-8 A=B
Step-9 B=NEXT
Step-10 COUNT=COUNT + 1
Step-11 Go to step-4
Step-12 Stop

Flowchart of Fibonacci Series:


Source code(without recursion):
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
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;
}
return 0;
}

With recursion:
#include<stdio.h>
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);
return 0;
}
Output:
Enter the number of elements:11

0 1 1 2 3 5 8 13 21 34 55
Process returned 0 (0x0) execution time : 8.870 s
Press any key to continue.

Discussions:
In this lab, we explored the Factorial and Fibonacci series, implemented them using recursive
and iterative methods. We observed the trade-offs between elegant recursive solutions and
efficient iterative implementations. The lab provided valuable insights into the use of data
structures and algorithms for solving mathematical problems and emphasized the importance of
optimizing recursive algorithms for better performance.

Vous aimerez peut-être aussi