You are on page 1of 7

ADA Assignment 2

Name : Prajwal Mate

MIS : 111915090

Batch : CSE

Question 1 : Finding a position of a particular number in a series of no.s using linear search and
binary search.

//Linear Search

#include<iostream>

using namespace std;

int main()

int num;

cout<<"\nEnter the number of elements in the array : ";

cin>>num;

int array[num];

cout<<"\nEnter the elements of the array : ";

for(int i=0;i<num;i++)

cin>>array[i];

int target;

cout<<"\nEnter the target value to be searched : ";

cin>>target;

int flag=0,i=0;

while(i<num)
{

if(array[i]==target)

flag=1;

break;

i++;

if(flag==1) cout<<"\nTarget value is at position : "<<i+1;

else cout<<"\nTarget value not present in the array";

return 0;

//Binary Search

#include<iostream>

using namespace std;

int main()

int num;

cout<<"\nEnter the number of elements in the array : ";

cin>>num;

int array[num];

cout<<"\nEnter the elements of the array : ";

for(int i=0;i<num;i++)
cin>>array[i];

int temp;

for(int i=num-1;i>=0;i--)

for(int j=1;j<=i;j++)

if(array[j-1]>array[j])

temp=array[j-1];

array[j-1]=array[j];

array[j]=temp;

int target;

cout<<"\nEnter the target value to be searched : ";

cin>>target;

int mid,left=0,right=num,flag=0,i=-1;

do{

mid=(left+right)/2;

i++;

if(array[mid]==target)

flag=1;

break;
}

else if(array[mid]>target)

right=mid-1;

else

left=mid+1;

}while(i<num);

if(flag==1) cout<<"\nTarget value is at position : "<<mid+1;

else cout<<"\nTarget value not present in the array";

return 0;

Question 2 : Display Fibonacci series for some Nth no.

#include<iostream>

using namespace std;

int main()

int a=0,b=1,c,num,i=0;

cout<<"\nEnter a number (>1): ";

cin>>num;

cout<<"\nThe Required Sequence of fibonacci series is : ";

cout<<a<<" "<<b<<" ";

while(i<num-2)

{
c=a+b;

a=b;

b=c;

i++;

cout<<c<<" ";

return 0;

Question 3 : Find Greatest Common Divisor (GCD) of two no.s.

#include<iostream>

using namespace std;

int func(int*,int);

int main()

int num1,num2;

cout<<"\nEnter two numbers : ";

cin>>num1>>num2;

int array1[num1],array2[num2],count1,count2;

count1=func(&array1[0],num1);

count2=func(&array2[0],num2);

do{
if(array1[count1]==array2[count2])

break;

else if(array1[count1]>array2[count2])

for(;count1>0;count1--)

if(array1[count1]<=array2[count2])

break;

else

for(;count2>0;count2--)

if(array2[count2]<=array1[count1])

break;

}while(1);

cout<<"\nThe Greatest Common Divisor of "<<num1<<" and "<<num2<<" is "<<array1[count1];

return 0;

int func(int* array,int num)

{
int count=0,i=1;

while(i<=num)

if(num%i==0)

array[count]=i;

count++;

i++;

return (count);

You might also like