Functions C Lab
Functions C Lab
A.S.Manikandan
22BCA0152
Write a C program to search a given number using binary search algorithm using functions.
#include <stdio.h>
int main() {
int arr[100],n,i,element;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
scanf("%d",&element);
if (index == -1) {
else {
return 0;
if (arr[mid] == num) {
return mid;
return -1;
Write a C Program to find highest common factor HCF of 4 given numbers using recursive function.
#include <stdio.h>
int main()
int A[25],B[25],i,s1,s2;
scanf("%d",&s1);
for(i=0;i<s1;i++)
scanf("%d",&A[i]);
scanf("%d",&s2);
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;
i++;
j++;
else {
i++;
j++;
while (i < m) {
i++;
while (j < n) {
j++;
printf("\n");
printf("\nSet intersection:");
int i = 0, j = 0;
i++;
j++;
else {
i++;
j++;
printf("\n");
printf("\nSet difference:");
int i = 0, j = 0;
i++;
j++;
else {
i++;
j++;
while (i < m) {
i++;
while (j < n) {
j++;
printf("\n");
Write a C Program to find highest common factor HCF of 4 given numbers using recursive function.
#include <stdio.h>
if (b == 0)
return a;
else
int main()
int 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() {
scanf("%s", first_name);
scanf("%s", middle_name);
scanf("%s", last_name);
strcpy(full_name, first_name);
strcat(full_name, middle_name);
strcat(full_name, last_name);
return 0;
#include <stdio.h>
#include <string.h>
int main() {
char names[10][50];
int n,i,j;
scanf("%d", &n);
scanf("%s", names[i]);
char temp[50];
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
printf("List of names in alphabetical order:\n");
printf("%s\n", names[i]);
return 0;