Monday, April 14, 2008

8.max-min problem

PROGRAM - VIII

(A) Problem Statement :

Write a program to implement maxmin problem by
using divide and conquer technique.

(B) Input and Output :

This program takes array of integers and gives the
maximum and minimum number in the array of integers.

(i) Enter no of elements in the list :5
Enter the element : 34
Enter the element : 67
Enter the element : 23
Enter the element : 56
Enter the element : 12
Maximum element is : 67
Minimum element is :12


(ii) Enter no of elements in the list :4
Enter the element :63
Enter the element : 72
Enter the element : 33
Enter the element : 56
Maximum element is : 72
Minimum element is : 33















(C) Examples :

(i)

- - 72 33







- - 12
12
-
- 67
23





23 56 56 23
12 - 12 12
34 67 67 34







33
56 56
33

(ii)






- - 67 12
63 72 72 63








(D)Algorithm:

Step 1: Start
Step 2: Read the n elements in to the array a.
Step 3: repeatedly do the following steps.
Step 4: if n is 1 then print max and min which is a[1] and
exit.
Step 5: if n is 2 then compare two elements and give max
and min values then print those values and exit.
Step 6: if n>2 then divide array into two sub arrays by
calculating mid = (beginning +end)/2
Step 7: find the maximum and minimum numbers in two
sub arrays in such way by dividing each array
into sub arrays until we can reach either step1 or
step2.
Step 8: combine sub arrays by giving maximum and
minimum numbers until we reach original array.
Step 9: Stop

(E) Program :

#include
#include
void maxmin(int a[],int i,int j,int *min,int *max)
{
int mid,max1,min1;
if(i==j) //if there is one element
*max=*min=a[i]; //min and max is same element
else if(i==j-1)
{ //if 2 elements
if(a[i]{
*max = a[j]; //and find min and max
*min = a[i];
}
else
{
*max = a[i];
*min = a[j];
}
}
else
{
// if p is not small, divide p into subproblems
// find where to spilt into the set
mid = (i+j)/2;
//solve the sub problems.
maxmin(a,i,mid,min,max);
maxmin(a,mid+1,j,&min1,&max1);
//combine the solutions
if(*max*max=max1;
if(*min>min1)
*min=min1;
}
}
void main()
{
int i,a[20],n,min,max;
clrscr(); //read no of elements in list
printf("\n enter the no of elements in the list:");
scanf("%d",&n);
for(i=1;i<=n;i++) //read elements
{
printf("\n enter the element:");
scanf("%d",&a[i]);
}
maxmin(a,1,n,&min,&max); //call maxmin function
printf("\n Maximum element is:%d",max); //print max
printf("\n Minimum element is:%d",min); //print min
getch();
}


(F) Testing :
This program can be tested to find maximum and
minimum value for the given list of size 20.


(G) Limitations:
(i) The maximum number of elements in the array is
restricted to 20 elements.

7.internal representation of intergers

PROGRAM - VII

(A) Problem Statement :

Write a program to display the internal representation of
integers.

(B) Input and Output :

This program takes an integer value as input and gives it’s
internal representation, that is if it is positive number then it’s
binary representation and if it is negative number then it’s 2’s
complement in 32-bits.

(i) enter the integer value : 12
00000000000000000000000000001100

(ii) enter the integer value : -6
11111111111111111111111111111010

(C) Examples :

(i) the internal representation of 12 is in binary form
• 12%2=0 and n=12/2=6
• 6%2 = 0 and n=6/2 =3
• 3%2 = 1 and n = 3/2 =1
• 1%2 = 1 and n = 1/2 = 0
The remaining bits are all 0’s
Internal representation is 000000000000000000000000000001100

(ii) the internal representation of -6 is in 2’s complement form
~(binary representation of 6)+1
• 6%2 = 0 and n=6/2 =3
• 3%2 = 1 and n = 3/2 =1
• 1%2 = 1 and n = 1/2 = 0
The binary equivalent for -6 is
000000000000000000000000000110
The one’s complement is
11111111111111111111111111111001
The two’s complement is
11111111111111111111111111111010

(D)Algorithm :

Step 1: start
Step 2: read the integer value n and initialize array a
of size 32 with 0’s.
Step 3: if n >0 then repeat the step4 and 5 till the
value of n is greater than 0 .
Step 4: start from I value 0 and increment it by 1
After step 5.
Step 5: a[i] = n%2 and n = n/2;
Step 6 : if n<0 then repeat the following step7 and 8
till the value of n is greater than 0 .
Step 7: start from I value 0 and increment it by 1
After step 5.
Step 8: a[i] = (n%2+1)%2 and n = n/2;
Step 9: print the array from 31 to 0.
Step 10: Stop

(E) Program :

#include
#include
void internal(long long int n)
{
int a[32]={0},i,j;
if(n>0) //if n is positive number
{
for(i=0;n>0;i++) //finds binary representation
{
a[i]=n%2;
n/=2;
}
}
if(n<0) //n is negative number
{
for(i=0;i<32;i++)
{
a[i]=((n%2)+1)%2; //finds 2's complement
n /=2;
}
for(i=0;i<32;i++) //and displays
{
a[i] = a[i]+1;
if(a[i]>1)
a[i]=0;
else
break;
}
}
for(j=31;j>=0;j--) //display for n>0
printf("%d",a[j]);
printf("\n");

}
void main()
{
long long int n;
clrscr(); //read the integer value
printf("\n enter the integer value:");
scanf("%lld",&n);
internal(n); //call internal function
getch();
}


(F) Testing :
The program can be tested for any integer value and
represented in 32-bit binary form.


(G) Limitations :
(i) it can give representation of 4294967295.

6.bubble sort, selection sort, Quick sort, Merge sort

PROGRAM - VI

(A) Problem Statement :

Create a structure with members as register number, name , marks. For each student in the class sort all the records by using bubble sort, selection sort, Quick sort, Merge sort.

(B) Input and Output :

(i) Enter student details :
Enter register no: 2
Enter name : balaji
Enter marks :89
Do you want to enter details of another student(1/0):1
Enter register no: 1
Enter name : aditya
Enter marks : 88
Do you want to enter details of another student(1/0):0

************ Student Details ***********
Register no Name Marks
Student 0: 2 balaji 89
Student 1: 1 aditya 88

1.bubble sort 2.selection sort 3.quick sort 4.mergesort

Enter your choice of technique : 1

************ Student Details ***********
Register no Name Marks
Student 0: 1 aditya 88
Student 1: 2 balaji 89


(ii) Enter student details:
Enter register no: 23
Enter name: srikanth
Enter marks: 49
Do you want to enter details of another student (1/0):1
Enter register no: 12
Enter name: adita
Enter marks: 84
Do you want to enter details of another student (1/0):1
Enter register no: 19
Enter name : miller
Enter marks : 67
Do you want to enter details of another student(1/0):0

************ Student Details ***********
Register no Name Marks
Student 0: 23 srikanth 49
Student 1: 12 adita 84
Student 2: 19 miller 67

1.bubble sort 2.selection sort 3.quick sort 4.mergesort

Enter your choice of technique : 1

************ Student Details ***********
Register no Name Marks
Student 0: 12 adita 84
Student 1: 19 miller 67
Student 2: 23 srikanth 49


(C) Examples :

(i) List is{ 23,12,19}
Selection sort:
S : 23, 12, 19
0 1 2
Min=0
Compare S[min] with s[1]and s[2] find correct min value
position
Min =1
Then s is s : 12 23 19
0 1 2
Min = 1
Compare S[min] with s[2] find correct minimum value
Position.
Min = 2
Then s is s : 12 19 23
0 1 2




(ii) the list is { 34 , 12 ,56,45 }
S: 34 , 12 , 56 ,45
Merge sort :

34 , 12 , 56 , 45


34,12 56 ,45


34 12 56 45



12,34 45, 56


12,34,45,56


(D)Algorithm :

Step 1: Start
Step 2: Define the structure student with regno, name and
marks.
Step 3: Read the no of students n to be sorted and
details into array of structure s.
Step 4: select the technique to be used to sort students
according to register no of student.
Step 5: if bubble sort is selected then the following step6
are repeated for I values 0 to n-1 then goto step15.
Step 6:for j value 0 to n-1 compare I student regno with
jth student regno if student I is greaer then
exchange details of I and j th students.
Step 7:if selection sort is selected do the following step I
values 0 to n-1and goto step15.
Step 8: find minimum number among them and exchange
With I and take lt as remaining.
Step 9:if quick sort is selected do the following step10,11
Until list contains single element goto step15.
Step 10: select the mid element in the list as key element
Place them in correct position in list.
Step 11:Apply step10 for two list obtained one before key
and after key.
Step 12:if merge sort is selected do the following step13,
14 and after that goto step 15.
Step 13:divide the list into sublist until each sublist
contains single element.
Step 14:merge each sublist and sort while merging until
We get single list.
Step 15:print sorted student details.
Step 16:Stop

(E) Program:

#include
#include
struct student //define the structure
{
int regno;
char name[20];
int marks;
};
void swapList(struct student *m,struct student *n)
{ //exchange the contenet of m and n
struct student temp;
temp = *m;
*m = *n;
*n = temp;
}
void display(struct student s[10],int n)
{ //display the students details
int i;
printf("\n********* student details are **********");
printf("\n\n\t\tRegisterNo \tName \t\tMarks");
for(i=0;i<=n;i++)
{
printf("\n student %d :",i);
printf("\t%d",s[i].regno);
printf("\t\t%s",s[i].name);
printf("\t\t%d",s[i].marks);
}
}
void bubblesort(struct student s[10],int n) //bubble sort
{
int i,j;
for(i=0;i for(j=0;j if(s[j].regno > s[j+1].regno)
swapList(&s[j],&s[j+1]); //exchange the s[j] and s[j+1]
}
void selectsort(struct student s[10],int n) //selection sort
{
int i,j,k,min;
for(i=0;i{ //intialize mininimum no position
min=i;
for(j=i+1;j<=n;j++)
{
if(s[j].regno min=j; //change min velue to j
}
swapList(&s[i],&s[min]); //exchange the contents at i and min
}
}
void quicksort(struct student s[10],int m,int n)
{ // quick sort
int key,i,j,k;
if( m < n)
{ //pick key element is mid element
k = (m+n)/2;
swapList(&s[m],&s[k]); //exchange contents at m and k
key = s[m].regno;
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (s[i].regno <= key))
i++;
while((j >= m) && (s[j].regno > key))
j--;
if( i < j)
swapList(&s[i],&s[j]);
}
swapList(&s[m],&s[j]); //position key element
quicksort(s,m,j-1); //call quick sort on first sub array
quicksort(s,j+1,n); //call quick sort on second sub array
}
}
void mergesort(struct student s[10],int start,int end)
{ //merge sort
int j = 0;
const int size = start - end + 1;
int mid = 0; //intialize mergelist 1 and 2 to 0
int mrg1 = 0;
int mrg2 = 0;
struct student executing[10];

if(end == start)
return;

mid = (end + start) / 2; //find mid position

mergesort(s, end, mid); //call mergsort first merge list
mergesort(s, mid + 1, start); //call mergesort for second list

for(j = 0; j < size; j++)
executing[j] = s[end + j];

mrg1 = 0; //merging lists
mrg2 = mid - end + 1;

for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1].regno > executing[mrg2].regno)
s[j + end] = executing[mrg2++];
else
s[j + end] = executing[mrg1++];
else
s[j + end] = executing[mrg2++];
else
s[j + end] = executing[mrg1++];
}
}

void main()
{
struct student s[10];
int c,i=-1;
clrscr();
do
{ //reading student details
++i;
printf("\n enter student details:");
printf("\n enter register no:");
scanf("%d",&s[i].regno);
printf("\n enter name:");
scanf("%s",&s[i].name);
printf("\n enter the marks");
scanf("%d",&s[i].marks);
printf("\n do you want to enter details of another student(1\0):");
scanf("%d",&c);
}while(c!=0);
display(s,i); //selecting sorting technique
printf("\n 1.bubble sort 2.selection sort 3.quick sort 4.merge sort");
printf("\n enter your choice techinique");
scanf("%d",&c);
switch(c)
{ //call bubble sort
case 1: bubblesort(s,i); //display student details
display(s,i);
break;
case 2: selectsort(s,i); //call selection sort
display(s,i); //display student details
break;
case 3: quicksort(s,0,i); //call quick sort
display(s,i); //display student details
break;
case 4: mergesort(s,0,i); //call mergesort
display(s,i); //display student details
break;
}
getch();
}


(F) Testing:
The program can be tested for the given list of students in
the form of structure and sorted according to roll number
properly for the given sorting techniques.


(G) Limitations:
(i) this program is limited to sort only 10 student records.

5.linear search & binary search

PROGRAM - V

(A) Problem Statement :

Write a program to search for a particular string
from given set of strings using linear search and binary search
techniques.

(B) Input and Output :

This program takes the set of strings and string to be
searched and technique used search the string in the given set
of strings.


(i) Enter string: balaji
Do you want to enter another string (y/n) y
Enter string: Jaya
Do you want to enter another string (y/n) y
Enter string: Vignesh
Do you want to enter another string (y/n) y
Enter string: Chanthi
Do you want to enter another string (y/n) n

balaji
Jaya
Vignesh
Chanthi

1. Linear search 2. Binary search

Enter the technique: 1
Enter the string to be searched: Vignesh
The string Vignesh is present at position 3 in list.



(ii) Enter string: balaji
Do you want to enter another string (y/n) y
Enter string: Jaya
Do you want to enter another string (y/n) y
Enter string: Vignesh
Do you want to enter another string (y/n) y
Enter string: Chanthi
Do you want to enter another string (y/n) n


balaji
Jaya
Vignesh
Chanthi


1.linear search 2. binary search

Enter the technique : 2
Enter the string to be searched : Vignesh

Sorted list of strings is
balaji
Chanthi
Jaya
Vignesh

The string vigneshis found at position 4 in sorted list.


(C) Examples :

(i) the list is { balaji,Jaya,Vignesh,Chanthi}
String to be searched (str): jaya
S: balaji,Jaya,Vignesh,Chanthi
0 1 2 3
Compare s[0] with str it is not equal
Compare s[1] with str it is equal.
The string jaya is at (1+1) 2nd position in the list.

(ii) the list which is sorted is { balaji, Chanthi, Vignesh }
String to be searched (str) :balaji
S: aruna, chinni, vidula
0 1 2
Mid = (start + end)/2 = (0+2)/2 = 1
Compare s[mid] and str.not equal and s[mid] Start = start = 0 and end = mid-1 = 1-1 =0
Mid = (start+end)/2 = 0
S[mid] = str element is found
The string is found at (0+1) position.



(D)Algorithm :

Step 1: Start
Step 2: Read the list of strings
Step 3: Read the technique used for search and
string to be searched.
Step 4: If the technique is
(a) linear search
(i) repeat the following steps (ii) and (iii)
from I value 0 to length of list- 1.
(ii) compare given string with I th string
in the list.
(iii) if string is found then print the
position of string in the list and exit.
(iv) if string not found print string not
found in the list.
(b) binary search
(i) sort the given list and print sorted list.
(ii)find the mid position in the list by
giving starting and ending positions in
the list.
(iii)if string is found at mid position print
the position and exit.
(iv)if string is less than string at mid
position the repeat steps (ii) and
(iii)by taking beginning and mid
position-1 as our new list.
(v) if string is greater than string at mid
position the repeat steps (ii) and
(iii)by taking mid+1 and end of list
as our new list.
(vi) if string not found print string not
found in the list.

(E) Program :

#include
#include
#include

int binarysearch(char s[20][20],char str[20],int arrayStart,int arrayEnd,int c)
{
int m,i,j;
char st[20];
if(c==1){
for(i=0;i {
for(j=1;j {
if(strcmp(s[j-1],s[j])>0)
{ //exchange of contents
strcpy(st,s[j-1]);
strcpy(s[j-1],s[j]);
strcpy(s[j],st);
}
} //end of sorting
}

printf("\n\nsorted list of strings"); //printing sorted list
for(i=0;i {
printf("\n%s",s[i]);
}
} //search begins
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2; //find the mid position
if (strcmp(s[m],str)==0) //compare the strings
return m;
else if (strcmp(str,s[m])<0) // call binary search in the one half of array
return binarysearch(s,str,arrayStart,m-1,-1);
else
return binarysearch(s,str,m+1,arrayEnd,-1);
}
return -1;
}

void linearsearch(char s[20][20],char str[20],int num)
{
int j, f=0;
for(j=0;j if( strcmp(s[j],str) == 0) //if element found print position
{
printf("\nThe string %s is present at position %d in list\n",str,j+1);
f=1;
break;
}
if(f==0) //if element not found then print
printf("\nThe string is %s is not present in the list\n",str);
}

void display(char s[20][20],int n)
{
int i; //display the strings
for(i=0;i {
printf("\n%s",s[i]);
}
}

void main()
{
char str[20],ch,s[20][20];
int n,i=0,c;
clrscr();
do
{ //read the strings
printf("\n enter the string:");
fflush(stdin);
scanf("%s",s[i++]);
printf("\n do you want to enter another string Y/N");
fflush(stdin);
scanf("%c",&ch);
n=i;
}while(ch!='n');
printf("\n n=%d",n);
display(s,n); //display the string list
printf("\n 1.linear search 2.binary search");
printf("\n enter your choice:"); //read the searching technique
scanf("%d",&c);
fflush(stdin);
printf("\n enter the string to be searched:");
scanf("%s",&str); //read the string to be searched
switch(c)
{
case 1:
linearsearch(s,str,n); //call linear search
break;

case 2: c=binarysearch(s,str,0,n,1); //call binary search
if(c==-1)
printf("\nstring not found in the list");
else
printf("\n the string %s is found at position %din sorted list",str,c+1);
break;
default : break;
}
getch();
}


(F) Testing :


S.NO Input Aspect to be Tested Expected Result
1 List of strings ={ba,jy,ch} Str = ba
String ar found at 1 position
2 List of strings ={ba,jy,ch} Str = ar String bg not found










(G) Limitations :
(i) the search list can have maximum of 20 strings and
each of which has maximum of 20 characters.
(ii) the length of string to be searched is restricted to 20
characters.

4.all possible permutations with out duplicates of given set

PROGRAM - IV

(A) Problem Statement :

Write a program to generate all possible
permutations with out duplicates of given set.

(B) Input and Output :

This problem will take n no of numbers as set and gives
all possible permutations all n numbers with out duplicates.

(i) enter no of digits: 3
Enter digit: 1
Enter digit: 2
Enter digit: 3
Permutations are :
1 2 3
1 3 2
2 3 1
2 1 3
3 1 2
3 2 1

(ii) enter no of digits: 3
Enter digit: 1
Enter digit: 2
Enter digit: 1
Permutations are :
1 2 1
1 1 2
2 1 1

(C) Examples:

(i) if the set contains 4,5,6 elements the permutations are
different arrangements of set elements.
4 5 6
4 6 5
5 4 6
5 6 4
6 4 5
6 5 4

(ii) if the set contains 4,5,4 elements then permutations are
4 5 4
4 4 5
5 4 4

(D)Algorithm :

Step 1: Start
Step 2: Read the no of elements of set into n and
the elements into array a. initialize k=p=0.
Step 3: repeat the following steps until p value is 2
power n.
Step 4:if k=n then check for duplicates and if there
is no duplicate then print array.
Step 5:otherwise for I value k to n repeat steps 6to8
Step 6:exchange the array element I and k.
Step 7: goto step 3 with k value increased by 1.
Step 8: interchange array elements at k and I .
Step 9: Stop

(E) Program:

#include
#include
int p=0;
void perm(int a[],int k,int n) //finding permutations
{
int i,t,j,c;
int x[25][15];
if(k==n) // output the permutations
{
for(i=0;i{
c=0;
for(j=1;j<=n;j++) //duplicates checking
{
if(x[i][j]==a[j])
{
c++;
}
}
if(c==n) break;
}
if(c!=n)
{
printf("\n");
for(i=1;i<=n;i++)
{ //Storing permutations in x
x[p][i]=a[i];
printf("\t%d",a[i]);
}
p=p+1;

}
}
else //a[k:n] has more than one permutation.
{ //Generate these recursively.
for(i=k;i<=n;i++)
{
t=a[k];
a[k]=a[i];
a[i]=t;
perm(a,k+1,n); //all permutations of a[k+1:n]
t=a[k];
a[k]=a[i];
a[i]=t;
}
}
}


void main()
{
int a[5],i,n;
clrscr(); //read the no of elements in set
printf("\n enter no of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++) //read the elements of set
{
printf("\n enter the element:");
scanf("%d",&a[i]);
}
perm(a,1,n); // call the prem function
getch();
}



(F) Testing:


S.NO Aspect to be Tested Expected Result
1 Set of elements ={1,2}
1 2
2 1
Successful
2 Set of elements = {1,1} 1 1
(Duplicates are eliminated)
3 Set of elements = {1,2,3,4,5} Abnormal termination
(due to out of range)










(G) Limitations :
(i) this program can take maximum of 4 numbers only.
(ii) this program can give permutations of 4 numbers only
since the array size is 25 rows.


3.all aubsets of given set

PROGRAM - III


(A) Problem Statement :

Write a program to find all subsets of the given set.

(B) Input and Output:

(i)Enter no of elements in the set :3
enter set element : c
enter set element :d
enter set element : e
All subsets are:
empty
e
d
de
c
ce
cd
cde


(ii)Enter no of elements in the set :3
enter set element : a
enter set element :b
All subsets are:
empty
a
b
ab

(C) Examples :


(i) if the set contains {a,b,c} then the subsets of the given
set is
{Ø},{a},{b},{c},{a,b},{a,c},{b,c},{a,b,c}.

(ii) if set is {4,5} then the subsets of given set is
{Ø},{4},{5},{4,5}.


(D)Algorithm :

Step 1: Start
Step 2: read the no of elements of set n and read set
Elements, take Boolean array b of size as set and
initialize with 0’s..
Step 3: repeat the following steps 4 to 6 for I value 0 to n
Step 4: assign nth value in b to 1.
Step 5:if n value is less than n-1 then print subset
Step 6:repeat the step 3 to 5 for i+1
Step 7:stop

(E) Program :

#include
#include

#define SIZE 32

void show_subset(bool b[], char c[]);
void rec_iterate(bool b[],int n, char c[]);

void main()
{
char c[32];
int i,n;
bool b[SIZE]={0}; //read the no of elementsin set
printf("\n enter no of elemtns in the set");
scanf("%d",&n);
printf("\nenter elements into the set:");
for(i=0;i {
printf("\n enter element:");
scanf("%c",&c[i]);
}
printf(“\n All subsets are:”);
rec_iterate(b,0,c); //call iterate functions

}

void show_subset(bool b[], char c[])
{
bool empty = true; //intialize empty to true
for(int i = 0; i < SIZE; ++i)
if(b[i] == 1) // if subset is found
{
empty = 0; // print subset
printf("%c",c[i]);
}
if(empty) //nullset is subset for all sets
printf("\nempty"); //print null set as empty

}

void rec_iterate(bool b[],int n, char c[])
{
for(int i = 0; i <= 1; ++i)
{
b[n] = i; //intialize with i value
if(n < SIZE -1)
rec_iterate(b, n + 1,c); //call iterate function
else
show_subset(b, c); //if subset found then
} //print by calling show_subset function
}


(F) Testing :

The program can be tested for the given set of elements
and for all its subsets.

(G) Limitations :

(i) this program can find power set of given set of size 32
only.

2.fibonacci number

PROGRAM - II

(A) Problem Statement :

Write a program to calculate nth Fibonacci
number such that the time complexity as O(n).

(B) Input and Output :

The program takes the n value as input and prints first n Fibonacci numbers and gives nth Fibonacci number.

(i) the input value is 10 then the expected output is

Fibonacci series up to 10th number is
1 1 2 3 5 8 13 21 34 55
10 th Fibonacci number is : 55

(ii) the input value is 4 then the expected output is

Fibonacci series up to 4th number is
1 1 2 3
4th Fibonacci number is : 3

(C) Examples :

(i) f10 is 10th Fibonacci number .

f0 = 0, f1 = 1
f2 = f0 + f1 = 0 + 1 = 1
f3 = f1 + f2 = 1 + 1 = 2
f4 = f2 + f3 = 1 + 2 = 3
f5 = f3 + f4 = 2 + 3 = 5
f6 = f4 + f5 = 3 + 5 = 8
f7 = f5 + f6 = 5 + 8 = 13
f8 = f6 + f7 = 8 + 13 = 21
f9 = f7 + f8 =13 + 21 = 34
f10= f8 + f9 =21 + 34 = 55

(i) f4 is 4th Fibonacci number
f0 = 0, f1 = 1
f2 = f0 + f1 = 0 + 1 = 1
f3 = f1 + f2 = 1 + 1 = 2
f4 = f2 + f3 = 1 + 2 = 3

(D)Algorithm :

Step 1: Start
Step 2: Read the n value
Step 3: initialize f0 and f1 to 1.
Step 4: Repeat the following steps from I value 2 to n
(i) f2 = f0 + f1
(ii) f0 = f1
(iii) f1 = f2
Step 5: print the nth Fibonacci number , which is in f2.
Step 6: Stop

(E) Program :

#include
#include
void fibn(long int n)
{
long int f3,f2,f1,i;
if(n<=1)
printf("\n%ld th fibonacci number is %ld",n); //First fibonacci number is 1
else
{
f2=0;
f1=1; //print the series up to nth fibonacci number
printf("\n fibonaci series upto %ld th number is :",n);
printf("\n%ld",f1); // print first number
for(i=2;i<=n;i++)
{
f3=f2+f1; //calculation of numbers
f2=f1;
f1=f3;
printf("\t%ld",f3); //print the fibonacci number
} //print nth fibonacci number
printf("\n\n %ld th fibonacci number is %ld",n,f3);
}
}
void main()
{
long int n;
clrscr();
do
{ // read the nth value
printf("\n enter which fibonacci number to be displayed(positive no):");
scanf("%ld",&n);
}while(n<0); // read only positive number
fibn(n); // call function
getch();
}


(G) Testing :
S.NO
Aspect to be Tested
Expected Result
1
N value =10
(nth Fibonacci number)
55
Successful
2
N value = -3
Fails
(enter positive number)
3
N value = 47
Wrong result
(due to out of range)








(H) Limitations :

(i) In this program the n is taken as long int . the range
of long int is 2,147,483,647.So, it can calculate up
to 46th Fibonacci number.