Co Lab Project 2
Co Lab Project 2
SUBMITTED BY:
MS. PRIYANKA ARORA
TANUJ
(24/A14/035)
Experment 11
Question: Write a program to find an element of an array
using linear search
CODE:
#include <stdio.h>
int main() {
int size, target, result;
int arr[size];
return 0;
}
OUTPUT:
Experiment 12
Question : Write a program to find an element of an array
using binary search.
Code:
#include <stdio.h>
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main() {
int size, target;
int arr[size];
printf("Enter sorted array elements: ");
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
if (result != -1)
printf("Element found at index %d\n",
result);
else
printf("Element not found\n");
return 0;
}
Output:
Experiment 13
Question:. Write a program to sort an array using bubble
sort.
Code:
#include <stdio.h>
int main() {
int n;
int arr[n];
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Experiment 14
Question: Write a program to sort an array using
selection sort.
Code:
#include <stdio.h>
minIndex = i;
for (j = i + 1; j < size; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
int main() {
int array[] = {64, 25, 12, 22, 11};
int size = sizeof(array) / sizeof(array[0]);
selectionSort(array, size);
return 0;
}
Output:
Experiment 15
Question: Write a program to sort an array using
insertion sort.
Code:
#include <stdio.h>
int main() {
int array[] = {12, 11, 13, 5, 6};
int size = sizeof(array) / sizeof(array[0]);
insertionSort(array, size);
return 0;
}
Output:
Experiment 16
Code:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1); // Recursive
case
}
}
int main() {
int num;
if (num < 0) {
printf("Factorial of a negative number
doesn't exist.\n");
} else {
Experiment 17
Code:
#include <stdio.h>
return length; }
int main() {
char str[100];
Experiment 18
Code:
#include <stdio.h>
str2++; }
*str1 = '\0';
}
int main() {
char str1[100];
char str2[50];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
concatenateStrings(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
Output:
Experiment 19
Question: Write a program to reverse a
given string.
Code:
#include <stdio.h>
int main() {
char str[100];
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}
Output:
Experiment 20
Question: Write a program to Addition of
matrix of 3x3 matrices.
Code:
#include <stdio.h>
int main() {
int matrix1[3][3], matrix2[3][3], sum[3][3];
int i, j;
return 0;
}
Output:
Experiment 21
Question: Write a program to
Multiplication of 3x3 matrices.
Code:
#include <stdio.h>
int main() {
int A[SIZE][SIZE] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int B[SIZE][SIZE] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
return 0;
}
Output:
Experiment 22
Question: Write a program to compare
length of strings.
CODE:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
compareStringLengths(str1, str2);
return 0;
}
OUTPUT:
Experiment 23
Question: )Write a program to find number
of vowels in a string.
CODE:
#include <stdio.h>
int main() {
char str[100];
return 0;
}
OUTPUT:
Experiment 24
Question: Write a program to print the
triangle of star
CODE:
#include <stdio.h>
int main() {
int rows, i, j;
return 0;
}
OUTPUT:
Experiment 25
Question: Write a program to find whether an
entered number is prime or not.
CODE:
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}}}
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
OUTPUT:
Experiment 26
Question: Write a program whether a string is
palindrome or not.
CODE:
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
int i, j, len;
if (strcmp(str, rev) == 0) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
OUTPUT:
Experiment 27
Question: Write a program to convert a string
from lower case to upper case
CODE:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i;
return 0;
}
OUTPUT:
Experiment 28
Question: Write a program to convert a string
from upper case to lower case and vice versa.
CODE:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i;
return 0;
}
OUTPUT:
Experiment 29
Question: )Write a program to swap two numbers
using pointers.
CODE:
#include <stdio.h>
int main() {
int num1, num2;
return 0
}
OUTPUT:
Experiment 30
Question: )Write a program to find the area
perimeter of a circle, rectangle, square and
triangle using functions.
CODE:
#include <stdio.h>
#include <math.h>
#define PI 3.14159
int main() {
float radius, length, width, side, base, height, a, b, c;
return 0:
}
OUTPUT:
Experiment 31
Question: Write a program to generate the
employee details using structure
CODE:
#include <stdio.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
int n, i;
printf("Enter the number of employees: ");
scanf("%d", &n);
printf("\nEmployee Details:\n");
for (i = 0; i < n; i++) {
displayEmployee(employees[i]);
printf("\n");
}
return 0;
}
OUTPUT:
Experiment 32
Question: Program to pass and return pointer to
function hence calculate average of an array.
CODE:
#include <stdio.h>
int main() {
int n;
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
OUTPUT:
Experiment 33
Question: Program to pass an array as pointer to
a function that calculates the sum of all elements
of the array.
CODE:
#include <stdio.h>
int calculateSum(int *arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int n;
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
OUTPUT:
Experiment 34
Question: . Program to demonstrate the example
of array of pointers
CODE:
#include <stdio.h>
int main() {
// Declare an array of pointers to char
const char *fruits[] = {
"Apple",
"Banana",
"Cherry",
"Date",
"Elderberry"
};
return 0;
}
OUTPUT:
Experiment 35
Question: Program to create a file called emp.txt
and store information about a person, in terms of
his name, age and salary.
CODE:
#include <stdio.h>
int main() {
char name[50];
int age;
float salary;
return 0;
}
OUTPUT:
Experiment 36
Question: 6. Program which copies one file
contents to another file.
CODE:
#include <stdio.h>
if (sourceFile == NULL) {
perror("Error opening source file");
return 1;
}
if (destinationFile == NULL) {
perror("Error opening destination file");
fclose(sourceFile);
return 1;
}
while ((bytesRead = fread(buffer, 1, sizeof(buffer),
sourceFile)) > 0) {
fwrite(buffer, 1, bytesRead, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
return 0;
}
OUTPUT:
Experiment 37
Question: Program to read a file and after
converting all lower case to upper case letters
write it to another file
CODE:
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *inputFile = fopen("input.txt", "r");
FILE *outputFile = fopen("output.txt", "w");
char ch;
if (inputFile == NULL) {
printf("Error opening input file.\n");
return 1;
}
if (outputFile == NULL) {
printf("Error opening output file.\n");
fclose(inputFile);
return 1;
}
OUTPUT:
Experiment 38
Question: Program to find the size of a given file
CODE:
#include <stdio.h>
int main() {
FILE *file = fopen("emp.txt", "rb");
if (file == NULL) {
printf("File not found.\n");
return 1;
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
fclose(file);
OUTPUT: