You are on page 1of 10

NAME:S.

RAGHUL SUBRAMANI
REG NO:19MIS1130
FACULTY NAME:KANCHANA
DEVI

1. Consider two single dimensional


Array of size 20 and 30 respectively.
Write a program in C to find out the
elements which are common in both
arrays.

INPUT:

#include<stdio.h>
#include<conio.h>
int main()
{
int a[10], b[10], c[10], i,j,m,n,k=0,x,count;
printf("Enter the number of elements in
the arrayA:");
scanf("%d",&m);
printf("Enter the elements for array A: ");
for(i=0; i<m; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number of elements in
the array B:");
scanf("%d",&n);
printf("Enter the elements for array B: ");
for(i=0;i<n;i++)
{
scanf("%d", &b[i]);
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if(a[i]==b[j])
{
count = 0;
for(x=0; x<k; x++)
{
if(a[i]==c[x])
count++;
}
if(count==0)
{
c[k]=a[i];
k++;
}
}
}
}
printf("\nThe elements common in both
arrays are :\n");
for(i=0; i<k; i++)
{
printf("%d ",c[i]);
}
gets()
return 0;

2. An array A contains 25 positive


integers. Write a program in C which
will find out the number of odd and
even numbers in that array.

INPUT:

//C Program to Count Even and Odd Numbers in


an Array
#include<stdio.h>
int CountEvenNumbers(int a[], int n);
int CountOddNumbers(int a[], int n);

int main()
{
int n,i,a[100];
int Even_Count=0,Odd_Count=0;

printf("\nEnter the number of elements in


the Array:\n");
scanf("%d", &n);

printf("\nEnter the elements in the array: ");


for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}

Even_Count = CountEvenNumbers(a,n);
Odd_Count = CountOddNumbers(a,n);

}
int CountEvenNumbers(int a[], int Size)
{
int i, Even_Count = 0;
printf("\nEven Numbers in this Array are: ");
for(i = 0; i < Size; i ++)
{
if(a[i] % 2 == 0)
{
printf("%d ", a[i]);
Even_Count++;
}
}
return Even_Count;
}
int CountOddNumbers(int a[], int Size)
{
int i, Odd_Count = 0;
printf("\nOdd Numbers in this Array are: ");

for(i = 0; i < Size; i ++)


{
if(a[i] % 2 != 0)
{
printf("%d ", a[i]);
Odd_Count++;
}
}
return Odd_Count;
}
3. The name roll number and marks
of 10 students are stored in an array
named marks. Write a program to
find the Roll Numbers of first and
second rank holders.

INPUT:

#include<stdio.h>
struct student
{
int rollno;
char name[100];
int score;
};
int main()
{
struct student s[20],temp;
int i,j,n;
printf("\nEnter no. of Students : ");
scanf("%d",&n);
printf("\nEnter the rollno,name,score
");
for(i=0;i<n;i++)
scanf("%d%s
%d",&s[i].rollno,s[i].name,&s[i].score);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(s[j].score<s[j+1].score)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}

printf("\nThe First and Second Rank


Holders are:\n");
for(j=1;j<n-1;j++)
printf("%d\n",s[j].rollno);
}

4. Consider the following list of


elements:
12, 4, 5,7,-5,8,10,14,16,15
Apply Linear search algorithm.
INPUT:

#include <stdio.h>
int main()
{
int array[100], search, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search) {
printf("%d is present at location %d.\n",
search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n",
search);
return 0;
}

(ii)Consider the following list of


elements:
12, 4, 5,7,-5,8,10,14,16,15
Apply Binary search algorithm.

INPUT:

#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first=0;
last=n-1;
middle=(first+last)/2;
while (first<=last) {
if (array[middle]<search)
first = middle + 1;
else if (array[middle]==search) {

printf("%d found at location %d.\n", search,


middle+1);
break;
}
else
last = middle-1;

middle = (first+last)/2;
}
if (first>last)
printf("Not found! %d isn't present in the
list.\n", search);
return 0;
}

You might also like