C PROGRAMMING LAB
A.S.Manikandan
22BCA0152
Write a C program to search a given number using binary search algorithm using functions.
#include <stdio.h>
int binary_search(int arr[], int left, int right, int num);
int main() {
int arr[100],n,i,element;
printf("Enter the array size");
scanf("%d",&n);
printf("\nEnter array elements in ascending order:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nEnter the element to be searched:");
scanf("%d",&element);
int num = element;
int index = binary_search(arr, 0, n-1, num);
if (index == -1) {
printf("%d not found in the array\n", num);
else {
printf("%d found at index %d\n", num, index);
}
return 0;
int binary_search(int arr[], int left, int right, int num) {
if (right >= left) {
int mid = left + (right - left) / 2;
if (arr[mid] == num) {
return mid;
if (arr[mid] > num) {
return binary_search(arr, left, mid-1, num);
return binary_search(arr, mid+1, right, num);
return -1;
Write a C Program to find highest common factor HCF of 4 given numbers using recursive function.
#include <stdio.h>
void set_union(int A[], int B[], int m, int n);
void set_intersection(int A[], int B[], int m, int n);
void set_difference(int A[], int B[], int m, int n);
int main()
int A[25],B[25],i,s1,s2;
printf("\n ENTER THE NUMBER OF ELEMENTS IN SET A");
scanf("%d",&s1);
printf("\n ENTER SET A : ");
for(i=0;i<s1;i++)
scanf("%d",&A[i]);
printf("\n ENTER THE NUMBER OF ELEMENTS IN SET B:");
scanf("%d",&s2);
printf("\n ENTER SET A : ");
for(i=0;i<s2;i++)
scanf("%d",&B[i]);
//SET UNION
set_union(A,B,s1,s2);
//SET INTERSECTION
set_intersection(A,B,s1,s2);
//SET DIFFERENCE
set_difference(A,B,s1,s2);
printf("\n\n");
}
void set_union(int A[], int B[], int m, int n) {
printf("Union of sets:");
int i = 0, j = 0;
while (i < m && j < n) {
if (A[i] < B[j]) {
printf("%d ", A[i]);
i++;
else if (B[j] < A[i]) {
printf("%d ", B[j]);
j++;
else {
printf("%d ", A[i]);
i++;
j++;
while (i < m) {
printf("%d ", A[i]);
i++;
while (j < n) {
printf("%d ", B[j]);
j++;
printf("\n");
void set_intersection(int A[], int B[], int m, int n) {
printf("\nSet intersection:");
int i = 0, j = 0;
while (i < m && j < n) {
if (A[i] < B[j]) {
i++;
else if (B[j] < A[i]) {
j++;
else {
printf("%d ", A[i]);
i++;
j++;
printf("\n");
void set_difference(int A[], int B[], int m, int n) {
printf("\nSet difference:");
int i = 0, j = 0;
while (i < m && j < n) {
if (A[i] < B[j]) {
printf("%d ", A[i]);
i++;
else if (B[j] < A[i]) {
printf("%d ", B[j]);
j++;
else {
i++;
j++;
while (i < m) {
printf("%d ", A[i]);
i++;
while (j < n) {
printf("%d ", B[j]);
j++;
printf("\n");
Write a C Program to find highest common factor HCF of 4 given numbers using recursive function.
#include <stdio.h>
int findHCF(int a, int b)
{
if (b == 0)
return a;
else
return findHCF(b, a % b);
int findHCF4(int a, int b, int c, int d)
int hcf1 = findHCF(a, b);
int hcf2 = findHCF(c, d);
return findHCF(hcf1, hcf2);
int main()
int a, b, c, d;
printf("Enter four numbers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
int hcf = findHCF4(a, b, c, d);
printf("The highest common factor of %d, %d, %d, and %d is: %d\n", a, b, c, d, hcf);
return 0;
}
The names of employees of a company are stored in three arrays, namely, first_ name, middle_name
and last_name. Write a C program to concatenate the three parts into a single name
#include <stdio.h>
#include <string.h>
int main() {
char first_name[50], middle_name[50], last_name[50];
printf("Enter first name: ");
scanf("%s", first_name);
printf("Enter middle name: ");
scanf("%s", middle_name);
printf("Enter last name: ");
scanf("%s", last_name);
// Concatenate the three names
char full_name[150];
strcpy(full_name, first_name);
strcat(full_name, " ");
strcat(full_name, middle_name);
strcat(full_name, " ");
strcat(full_name, last_name);
printf("Full name: %s\n", full_name);
return 0;
Write a c program to print the list of names in alphabetical order.
#include <stdio.h>
#include <string.h>
int main() {
char names[10][50];
int n,i,j;
printf("Enter the number of names : ");
scanf("%d", &n);
for ( i = 0; i < n; i++) {
printf("Enter name %d: ", i+1);
scanf("%s", names[i]);
char temp[50];
for ( i = 0; i < n-1; i++) {
for ( j = i+1; j < n; j++) {
if (strcmp(names[i], names[j]) > 0) {
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
printf("List of names in alphabetical order:\n");
for ( i = 0; i < n; i++) {
printf("%s\n", names[i]);
return 0;