Sei sulla pagina 1di 3

Sample Algorithms

(Part-2)
TO GENERATE THE FIBONACCI SERIES & FINDING THE
PRIME NUMBERS
STATEMENT:

In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci


sequence are the numbers in the following integer sequence.By definition, the
first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent
number is the sum of the previous two.In mathematical terms, the sequence Fn of
Fibonacci numbers is defined by the recurrence relation
with seed values,
Every 3rd number of the Fibonacci sequence is even.
A prime number (or a prime) is a natural number greater than 1 that has no
positive divisors other than 1 and itself. A natural number greater than 1 that is
not a prime number is called a composite number. For example, 5 is prime because
only 1 and 5 evenly divide it, whereas 6 is composite because it has the divisors 2
and 3 in addition to 1 and 6. Here by the program the prime numbers will be
generated frm the Fibonacci series.

ALGORITHM:

INPUT: A certain number of terms or elements ‘n’.

OUTPUT: Fibonacci series with ‘n’ elements and generating prime numbers
from the Fibonacci series.

PROCESS:

Step 1: Create the function “void main()”

Step 1.1: Take the number of terms from user in an integer type variable ‘n’.

Step 1.2: Print initial two Fibonacci numbers ‘0’ and ‘1’ which were
initialized to two integer type variable ‘a’ and ‘b’ i.e. print “a , b”.

Step 1.3: For i=0 to (n-3) repeat Step 1.4 to Step 1.14

Step 1.4: Set c  a+b

Step 1.5: Print “c”

Notes By: Prof.Amitav Biswas,HOD,CS Dept.,Behala College Page 1


Step 1.6: Set p  c

Step 1.7: If(p>1) then do

Step 1.8: For j=2 to (p/2) repeat Step 1.9

Step 1.9: If (p mod j=0) then

Break
[End of ‘If’]

[End of Step 1.8 ‘For’ loop]

Step 1.10: If ( j>p/2) then

Step 1.11: Set d[k]  p

Step 1.12: Set k  k+1

[End of Step 1.10 ‘If]

[End of Step 1.7 ‘If’]

Step 1.13: Set a  b

Step 1.14: Set b  c

[End of Step 1.3 ‘For’ loop]

Step 1.15: Print the integer type array ‘d’ to display the prime numbers
generated by the Fibonacci series.

[End of the function ‘void main()”]

Step 2: Stop

PROGRAM CODE:

/* FIBONACCI NUMBER */

#include<stdio.h>
#include<conio.h>

void main()
{
int a=0,b=1,i,c,n,p,j,d[50],k=0;
printf("Enter the number of terms:");

Notes By: Prof.Amitav Biswas,HOD,CS Dept.,Behala College Page 2


scanf("%d",&n);

printf("\n\nThe Fibonacci numbers are:\n\n%d %d ",a,b);

for(i=0;i<n-2;i++)
{
c=a+b;
printf("%d ",c);
p=c;
if(p>1)
{
for(j=2;j<=p/2;j++)
{
if(p%j==0)
break;
}
if(j>p/2)
{
d[k]=p;
k++;
}
}
a=b;
b=c;
}

printf("\n\nThe prime numbers of the fibonacci series are: ");

for(j=0;j<k;j++)
printf("%d, ",d[j]);

getch( );
}

Notes By: Prof.Amitav Biswas,HOD,CS Dept.,Behala College Page 3

Potrebbero piacerti anche