0% found this document useful (0 votes)
12 views49 pages

Co Lab Project 2

ksf

Uploaded by

rockstart22005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views49 pages

Co Lab Project 2

ksf

Uploaded by

rockstart22005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

SYBMITTED TO:

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 linearSearch(int arr[], int size, int target) {


for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}

int main() {
int size, target, result;

printf("Enter the number of elements in the


array: ");
scanf("%d", &size);

int arr[size];

printf("Enter %d elements:\n", size);


for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the element to search for: ");


scanf("%d", &target);

result = linearSearch(arr, size, target);


if (result == -1) {
printf("Element not found in the array.\n");
} else {
printf("Element found at index %d.\n",
result);
}

return 0;
}

OUTPUT:
Experiment 12
Question : Write a program to find an element of an array
using binary search.

Code:
#include <stdio.h>

int binarySearch(int arr[], int low, int high, int


target) {
while (low <= high) {
int mid = (low + high) / 2;

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;

printf("Enter array size: ");


scanf("%d", &size);

int arr[size];
printf("Enter sorted array elements: ");
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);

printf("Enter target: ");


scanf("%d", &target);
int result = binarySearch(arr, 0, size - 1,
target);

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>

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);


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

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>

void selectionSort(int array[], int size) {


int i, j, minIndex, temp;

for (i = 0; i < size - 1; i++) {

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;
}
}

void printArray(int array[], int size) {


int i;
for (i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
}

int main() {
int array[] = {64, 25, 12, 22, 11};
int size = sizeof(array) / sizeof(array[0]);

printf("Original array: \n");


printArray(array, size);

selectionSort(array, size);

printf("Sorted array: \n");


printArray(array, size);

return 0;
}

Output:
Experiment 15
Question: Write a program to sort an array using
insertion sort.
Code:
#include <stdio.h>

void insertionSort(int array[], int size) {


int i, key, j;
for (i = 1; i < size; i++) {
key = array[i];
j = i - 1;

while (j >= 0 && array[j] > key) {


array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
}
}

void printArray(int array[], int size) {


int i;
for (i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
}

int main() {
int array[] = {12, 11, 13, 5, 6};
int size = sizeof(array) / sizeof(array[0]);

printf("Original array: \n");


printArray(array, size);

insertionSort(array, size);

printf("Sorted array: \n");


printArray(array, size);

return 0;
}

Output:
Experiment 16

Question: Write a program to find


factorial using recursion

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;

printf("Enter a number: ");


scanf("%d", &num);

if (num < 0) {
printf("Factorial of a negative number
doesn't exist.\n");
} else {

printf("Factorial of %d is %d\n", num,


factorial(num));
}
return 0;
}
Output:

Experiment 17

Question: Write a program to find length


of a string without using predefined
function.

Code:
#include <stdio.h>

int stringLength(char str[]) {


int length = 0;

while (str[length] != '\0') {


length++; }

return length; }

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

int length = stringLength(str);

printf("Length of the string is: %d\n", length);


return 0;
}
Output:

Experiment 18

Question: Write a program to concatenates


a string without using predefined
libraries.

Code:
#include <stdio.h>

void concatenateStrings(char *str1, const char *str2)


{

while (*str1 != '\0') {


str1++;
}

while (*str2 != '\0') {


*str1 = *str2;
str1++;

str2++; }

*str1 = '\0';
}

int main() {
char str1[100];
char str2[50];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);

printf("Enter the second string: ");


fgets(str2, sizeof(str2), stdin);

str1[strcspn(str1, "\n")] = '\0'; // For str1


str2[strcspn(str2, "\n")] = '\0';

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>

void reverseString(char *str) {


char temp;
int start = 0;
int end = 0;

while (str[end] != '\0') {


end++;
}
end--;

while (start < end) {


temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';

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;

printf("Enter elements of first matrix (3x3):");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &matrix1[i][j]);
}
}

printf("Enter elements of second matrix (3x3):");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &matrix2[i][j]);
}
}

for (i = 0; i < 3; i++) {


for (j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i]
[j];
}
}

printf("Sum of the matrices:");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

return 0;
}
Output:
Experiment 21
Question: Write a program to
Multiplication of 3x3 matrices.

Code:
#include <stdio.h>

#define SIZE 3 // Define the size of the matrix

void matrix_multiply(int A[SIZE][SIZE], int B[SIZE]


[SIZE], int C[SIZE][SIZE]) {
// Initialize the result matrix C with zeros
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
C[i][j] = 0;
}
}

// Perform matrix multiplication


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
for (int k = 0; k < SIZE; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}

void print_matrix(int matrix[SIZE][SIZE]) {


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

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}
};

int C[SIZE][SIZE]; // Resultant matrix

// Multiply matrices A and B, store result in C


matrix_multiply(A, B, C);

// Print the resulting matrix C


printf("Resulting Matrix C:\n");
print_matrix(C);

return 0;
}
Output:

Experiment 22
Question: Write a program to compare
length of strings.
CODE:
#include <stdio.h>
#include <string.h>

void compareStringLengths(const char *str1, const


char *str2) {
int length1 = strlen(str1);
int length2 = strlen(str2);

if (length1 > length2) {


printf("The first string is longer by %d
characters.\n", length1 - length2);
} else if (length1 < length2) {
printf("The second string is longer by %d
characters.\n", length2 - length1);
} else {
printf("Both strings are of equal length.\
n");
}
}

int main() {
char str1[100], str2[100];

printf("Enter the first string: ");


fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0';

printf("Enter the second string: ");


fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0';

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 countVowels(const char *str) {


int count = 0;
while (*str) {
char ch = *str;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch
== 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch
== 'O' || ch == 'U') {
count++;
}
str++;
}
return count;
}

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
int vowelCount = countVowels(str);
printf("Number of vowels: %d\n", vowelCount);

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;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for(i = 1; i <= rows; i++) {


for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

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;

printf("Enter a number: ");


scanf("%d", &num);

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;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
len = strlen(str);
if (str[len - 1] == '\n') {
str[len - 1] = '\0';
len--;
}

for (i = 0, j = len - 1; i < len; i++, j--) {


rev[i] = str[j];
}
rev[len] = '\0';

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;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

for (i = 0; str[i] != '\0'; i++) {


if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 'a' + 'A';
}
}

printf("Uppercase string: %s", str);

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;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

for (i = 0; str[i] != '\0'; i++) {


if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 'a' + 'A';
} else if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] - 'A' + 'a';
}
}

printf("Converted string: %s", str);

return 0;
}

OUTPUT:

Experiment 29
Question: )Write a program to swap two numbers
using pointers.

CODE:
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int num1, num2;

// Input two numbers


printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

// Display numbers before swapping


printf("Before swapping: num1 = %d, num2 = %d\n", num1,
num2);

// Swap the numbers


swap(&num1, &num2);

// Display numbers after swapping


printf("After swapping: num1 = %d, num2 = %d\n", 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

float circleArea(float radius) {


return PI * radius * radius;
}

float circlePerimeter(float radius) {


return 2 * PI * radius;
}

float rectangleArea(float length, float width) {


return length * width;
}

float rectanglePerimeter(float length, float width) {


return 2 * (length + width);
}

float squareArea(float side) {


return side * side;
}

float squarePerimeter(float side) {


return 4 * side;
}

float triangleArea(float base, float height) {


return 0.5 * base * height;
}

float trianglePerimeter(float a, float b, float c) {


return a + b + c;
}

int main() {
float radius, length, width, side, base, height, a, b, c;

printf("Enter radius of circle: ");


scanf("%f", &radius);
printf("Circle Area: %.2f\n", circleArea(radius));
printf("Circle Perimeter: %.2f\n", circlePerimeter(radius));

printf("Enter length and width of rectangle: ");


scanf("%f %f", &length, &width);
printf("Rectangle Area: %.2f\n", rectangleArea(length,
width));
printf("Rectangle Perimeter: %.2f\n",
rectanglePerimeter(length, width));

printf("Enter side of square: ");


scanf("%f", &side);
printf("Square Area: %.2f\n", squareArea(side));
printf("Square Perimeter: %.2f\n", squarePerimeter(side));

printf("Enter base and height of triangle: ");


scanf("%f %f", &base, &height);
printf("Triangle Area: %.2f\n", triangleArea(base, height));

printf("Enter lengths of triangle sides: ");


scanf("%f %f %f", &a, &b, &c);
printf("Triangle Perimeter: %.2f\n", trianglePerimeter(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;
};

void displayEmployee(struct Employee emp) {


printf("Employee ID: %d\n", emp.id);
printf("Employee Name: %s\n", emp.name);
printf("Employee Salary: %.2f\n", emp.salary);
}

int main() {
int n, i;
printf("Enter the number of employees: ");
scanf("%d", &n);

struct Employee employees[n];

for (i = 0; i < n; i++) {


printf("Enter details for employee %d:\n", i + 1);
printf("ID: ");
scanf("%d", &employees[i].id);
printf("Name: ");
scanf(" %[^\n]s", employees[i].name);
printf("Salary: ");
scanf("%f", &employees[i].salary);
}

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>

float calculateAverage(int *arr, int size) {


int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return (float)sum / size;
}

int main() {
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

float average = calculateAverage(arr, n);


printf("Average: %.2f\n", average);

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;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int sum = calculateSum(arr, n);


printf("Sum of all elements: %d\n", sum);
return 0;
}

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"
};

// Calculate the number of elements in the array


int numberOfFruits = sizeof(fruits) / sizeof(fruits[0]);

// Print each fruit using the array of pointers


printf("List of fruits:\n");
for (int i = 0; i < numberOfFruits; i++) {
printf("%s\n", fruits[i]);
}

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;

printf("Enter name: ");


scanf("%49s", name);
printf("Enter age: ");
scanf("%d", &age);
printf("Enter salary: ");
scanf("%f", &salary);

FILE *file = fopen("emp.txt", "w");


fprintf(file, "Name: %s\n", name);
fprintf(file, "Age: %d\n", age);
fprintf(file, "Salary: %.2f\n", salary);
fclose(file);

return 0;
}
OUTPUT:
Experiment 36
Question: 6. Program which copies one file
contents to another file.

CODE:
#include <stdio.h>

int main(int argc, char *argv[]) {


if (argc != 3) {
printf("Usage: %s <source_file> <destination_file>\n",
argv[0]);
return 1;
}

FILE *sourceFile = fopen(argv[1], "rb");


FILE *destinationFile = fopen(argv[2], "wb");
char buffer[1024];
size_t bytesRead;

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;
}

while ((ch = fgetc(inputFile)) != EOF) {


fputc(toupper(ch), outputFile);
}
fclose(inputFile);
fclose(outputFile);
return 0;
}

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);

printf("Size of file: %ld bytes\n", size);


return 0;
}

OUTPUT:

You might also like