[go: up one dir, main page]

0% found this document useful (0 votes)
17 views25 pages

IntegratedLabManual1 1 2025

A

Uploaded by

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

IntegratedLabManual1 1 2025

A

Uploaded by

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

P.E.S.

COLLEGE OF ENGINEERING
(An Autonomous Institution Affiliated to VTU, Belagavi)

MASTER OF COMPUTER APPLICATIONS (MCA)

I SEMESTER

Programming and Problem Solving in C Laboratory


Integrated LAB MANUAL
Course Code: P24MCA11
Academic Year: 2024-2025
Lab Experiments: 10 Hours

Sl. No. Program Description Page No


1 Simulation of a Simple Calculator.
2 Implement Binary Search on Integers
3 Sort the given set of N numbers using Bubble sort.
4 Implement Matrix multiplication and validate the rules of
multiplication.
5 An electricity board charges the following rates for the use of
electricity: for the first 200 units 80 paise per unit: for the next 100
units 90 paise per unit: beyond 300 units Rs 1 per unit. All users are
charged a minimum of Rs. 100 as meter charge. If the total amount is
more than Rs 400, then an additional surcharge of 15% of total amount
is charged. Write a program to read the name of the user, number of
units consumed and print out the charges.
6 Write functions to implement string operations such as compare,
concatenate, and find string length. Use the parameter passing
techniques.
7 Implement structures to read, write and compute average- marks of the
students, list the students scoring above and below the average marks
for a class of N students.

8 Write a C program to copy a text file to another, read both the input file
name and target file name.
1. Simulation of a Simple Calculator.

/* 1. Simulation of a Simple Calculator */

#include <stdio.h>

int main() {

char operator;

double num1, num2, result;

// Display menu

printf("Simple Calculator\n");

printf("Enter an operator (+, -, *, /): ");

scanf(" %c", &operator);

// Input numbers

printf("Enter two numbers: ");

scanf("%lf %lf", &num1, &num2);

// Perform calculation

switch (operator) {

case '+':

result = num1 + num2;

printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);

break;

case '-':

result = num1 - num2;

printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);

break;

case '*':

result = num1 * num2;

printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);

break;

case '/':
if (num2 != 0) {

result = num1 / num2;

printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);

} else {

printf("Error: Division by zero is not allowed.\n");

break;

default:

printf("Error: Invalid operator.\n");

break;

return 0;

Output

Example Outputs:

 Addition Example: If the user enters + for the operator and inputs 5 and 3 as the two
numbers, the output will be:

Simple Calculator

Enter an operator (+, -, *, /): +

Enter two numbers: 5 3

5.00 + 3.00 = 8.00

Subtraction Example: If the user enters - for the operator and inputs 10 and 4 as the two
numbers, the output will be:

Simple Calculator

Enter an operator (+, -, *, /): -

Enter two numbers: 10 4

10.00 - 4.00 = 6.00

Multiplication Example: If the user enters * for the operator and inputs 7 and 2 as the two
numbers, the output will be:

Simple Calculator
Enter an operator (+, -, *, /): *

Enter two numbers: 7 2

7.00 * 2.00 = 14.00

Division Example: If the user enters / for the operator and inputs 8 and 4 as the two
numbers, the output will be:

Simple Calculator

Enter an operator (+, -, *, /): /

Enter two numbers: 8 4

8.00 / 4.00 = 2.00

Division by Zero Example: If the user enters / for the operator and inputs 8 and 0 as the two
numbers, the output will be:

Simple Calculator

Enter an operator (+, -, *, /): /

Enter two numbers: 8 0

Error: Division by zero is not allowed.

Invalid Operator Example: If the user enters an invalid operator, such as #, the output will
be:

Simple Calculator

Enter an operator (+, -, *, /): #

Error: Invalid operator.


2. Implement Binary Search on Integers

/* 2.Implement Binary Search on Integers */

#include <stdio.h>

// Function to perform binary search

int binarySearch(int arr[], int size, int target) {

int low = 0, high = size - 1;

while (low <= high) {

int mid = low + (high - low) / 2;

// Check if target is present at mid

if (arr[mid] == target)

return mid;

// If target is greater, ignore the left half

if (arr[mid] < target)

low = mid + 1;

else // If target is smaller, ignore the right half

high = mid - 1;

// Target not found

return -1;

int main() {

int arr[] = {2, 3, 4, 10, 40}; // Array must be sorted

int size = sizeof(arr) / sizeof(arr[0]);

int target;

// Input target value

printf("Enter the number to search: ");

scanf("%d", &target);

// Perform binary search


int result = binarySearch(arr, size, target);

if (result != -1)

printf("Element found at index %d\n", result);

else

printf("Element not found in the array\n");

return 0;

Output:

Example 1: User input 10

Enter the number to search: 10

Element found at index 3

Explanation: The target 10 is at index 3 in the sorted array {2, 3, 4, 10, 40}

Example 2: User input 5

Enter the number to search: 5

Element not found in the array

Explanation: The target 5 is not present in the array.

Example 3: User input 40

Enter the number to search: 40 Element found at index 4

Explanation: The target 40 is at index 4 in the sorted array.


3. Sort the given set of N numbers using Bubble sort.

/* 3. Sort the given set of N numbers using Bubble sort. */

#include <stdio.h>

// Function to perform bubble sort

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]) {

// Swap arr[j] and arr[j+1]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

int main() {

int n;

// Input the number of elements

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

scanf("%d", &n);

int arr[n];

// Input the elements of the array

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

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

scanf("%d", &arr[i]);

// Perform bubble sort


bubbleSort(arr, n);

// Output the sorted array

printf("Sorted array: \n");

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

printf("%d ", arr[i]);

Output :

Let's assume the user inputs the following:

Enter the number of elements: 5

Enter 5 numbers:

53842

Step-by-Step Execution:

1. The user inputs 5 as the number of elements.

2. The user then inputs the array: [5, 3, 8, 4, 2].

3. Bubble Sort Process:

o In the first pass:

 Compare 5 and 3 → swap them → [3, 5, 8, 4, 2]

 Compare 5 and 8 → no swap → [3, 5, 8, 4, 2]

 Compare 8 and 4 → swap them → [3, 5, 4, 8, 2]

 Compare 8 and 2 → swap them → [3, 5, 4, 2, 8]

o In the second pass:

 Compare 3 and 5 → no swap → [3, 5, 4, 2, 8]

 Compare 5 and 4 → swap them → [3, 4, 5, 2, 8]

 Compare 5 and 2 → swap them → [3, 4, 2, 5, 8]

o In the third pass:

 Compare 3 and 4 → no swap → [3, 4, 2, 5, 8]

 Compare 4 and 2 → swap them → [3, 2, 4, 5, 8]

o In the fourth pass:


 Compare 3 and 2 → swap them → [2, 3, 4, 5, 8]

Now, the array is sorted: [2, 3, 4, 5, 8]

Sorted array:

23458

Conclusion:

The output of the program, after the user inputs 5 and the numbers 5 3 8 4 2, will be:

Sorted array:

23458
4. Implement Matrix multiplication and validate the rules of multiplication.
/* 4. Implement Matrix multiplication and validate the rules of multiplication*/

#include <stdio.h>

// Function to multiply matrices

void multiplyMatrices(int row1, int col1, int mat1[row1][col1], int row2, int col2, int
mat2[row2][col2], int result[row1][col2]) {

for (int i = 0; i < row1; i++) {

for (int j = 0; j < col2; j++) {

result[i][j] = 0; // Initialize the result element

for (int k = 0; k < col1; k++) {

result[i][j] += mat1[i][k] * mat2[k][j];

int main() {

int row1, col1, row2, col2;

// Input dimensions of the first matrix

printf("Enter the number of rows and columns of the first matrix: ");

scanf("%d %d", &row1, &col1);

// Input dimensions of the second matrix

printf("Enter the number of rows and columns of the second matrix: ");

scanf("%d %d", &row2, &col2);

// Validate multiplication rules

if (col1 != row2) {
printf("Matrix multiplication not possible. Number of columns in the first matrix must
equal the number of rows in the second matrix.\n");

return 1;
}
int mat1[row1][col1], mat2[row2][col2], result[row1][col2];

// Input elements of the first matrix

printf("Enter elements of the first matrix:\n");

for (int i = 0; i < row1; i++) {

for (int j = 0; j < col1; j++) {

scanf("%d", &mat1[i][j]);

// Input elements of the second matrix

printf("Enter elements of the second matrix:\n");

for (int i = 0; i < row2; i++) {

for (int j = 0; j < col2; j++) {

scanf("%d", &mat2[i][j]);

// Multiply matrices

multiplyMatrices(row1, col1, mat1, row2, col2, mat2, result);

// Output the resulting matrix

printf("Resulting matrix after multiplication:\n");

for (int i = 0; i < row1; i++) {

for (int j = 0; j < col2; j++) {

printf("%d ", result[i][j]);

printf("\n");

return 0;

}
Output :
Let's consider an example where the user provides the following input:
Matrix 1: 2x3 matrix:

123

456

Matrix 2: 3x2 matrix:

78

9 10

11 12

Input:

Enter the number of rows and columns of the first matrix: 2 3

Enter the number of rows and columns of the second matrix: 3 2

Enter elements of the first matrix:

123

456

Enter elements of the second matrix:

78

9 10

11 12

Output:

The resulting matrix will be a 2x2 matrix (since 2x3 matrix multiplied by 3x2 matrix results
in a 2x2 matrix):

Resulting matrix after multiplication:

58 64

139 154

This output is derived as follows:


 First row, first column: (1 * 7) + (2 * 9) + (3 * 11) = 7 + 18 + 33 = 58
 First row, second column: (1 * 8) + (2 * 10) + (3 * 12) = 8 + 20 + 36 = 64
 Second row, first column: (4 * 7) + (5 * 9) + (6 * 11) = 28 + 45 + 66 = 139
 Second row, second column: (4 * 8) + (5 * 10) + (6 * 12) = 32 + 50 + 72 = 154
5. An electricity board charges the following rates for the use of electricity: for the
first 200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond
300 units Rs 1 per unit. All users are charged a minimum of Rs. 100 as meter
charge. If the total amount is more than Rs 400, then an additional surcharge of
15% of total amount is charged. Write a program to read the name of the user,
number of units consumed and print out the charges.

#include <stdio.h>

#include <string.h>

int main() {

char name[50];

int units;

float totalAmount = 0, surcharge = 0;

// Input user details

printf("Enter user name: ");

fgets(name, sizeof(name), stdin);

name[strcspn(name, "\n")] = 0; // Remove newline character from input

printf("Enter the number of units consumed: ");

scanf("%d", &units);

// Calculate charges based on units consumed

if (units <= 200) {

totalAmount = units * 0.80;

} else if (units <= 300) {

totalAmount = (200 * 0.80) + ((units - 200) * 0.90);

} else {

totalAmount = (200 * 0.80) + (100 * 0.90) + ((units - 300) * 1.00);

}
// Add minimum meter charge

if (totalAmount < 100) {

totalAmount = 100;

// Add surcharge if applicable

if (totalAmount > 400) {

surcharge = totalAmount * 0.15;

totalAmount += surcharge;

// Print the bill

printf("\nElectricity Bill\n");

printf("-------------------------------\n");

printf("User Name : %s\n", name);

printf("Units Consumed : %d\n", units);

printf("Surcharge : Rs %.2f\n", surcharge);

printf("Total Amount : Rs %.2f\n", totalAmount);

return 0;

Output :

Input:

Enter username: John Doe

Enter the number of units consumed: 150

Output:

Electricity Bill

-------------------------------
User Name : John Doe

Units Consumed : 150

Surcharge : Rs 0.00

Total Amount : Rs 120.00

Explanation:

 Since the units are less than 200, the charge is Rs 0.80 per unit.

 Total = 150 * 0.80 = Rs 120.00.

 Since the total is above Rs 100, no additional charges or surcharge are added.
6. Write functions to implement string operations such as compare,
concatenate, and find string length. Use the parameter passing techniques.

/* 6. Write functions to implement string operations such as compare, concatenate, and


find string length. Use the parameter passing techniques*/

#include <stdio.h>

#include <string.h>

// Function to compare two strings

int compareStrings(const char *str1, const char *str2) {

while (*str1 && *str2) {

if (*str1 != *str2) {

return *str1 - *str2;

str1++;

str2++;

return *str1 - *str2;

// Function to concatenate two strings

void concatenateStrings(char *dest, const char *src) {

while (*dest) {

dest++;

while (*src) {

*dest = *src;

dest++;

src++;

*dest = '\0';

}
// Function to find the length of a string

int stringLength(const char *str) {

int length = 0;

while (*str) {

length++;

str++;

return length;

int main() {

char str1[100], str2[100], result[200];

// Input strings

printf("Enter the first string: ");

fgets(str1, sizeof(str1), stdin);

str1[strcspn(str1, "\n")] = 0; // Remove newline character

printf("Enter the second string: ");

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

str2[strcspn(str2, "\n")] = 0; // Remove newline character

// String comparison

int cmpResult = compareStrings(str1, str2);

if (cmpResult == 0) {

printf("The strings are equal.\n");

} else if (cmpResult > 0) {

printf("The first string is greater than the second string.\n");

} else {

printf("The first string is less than the second string.\n");

// String concatenation
strcpy(result, str1); // Copy first string to result

concatenateStrings(result, str2); // Concatenate second string

printf("Concatenated string: %s\n", result);

// String lengths

printf("Length of the first string: %d\n", stringLength(str1));

printf("Length of the second string: %d\n", stringLength(str2));

return 0;

Output:

Example 1 (Strings are equal):

Input:

Enter the first string: apple

Enter the second string: apple

Output:

The strings are equal.

Concatenated string: appleapple

Length of the first string: 5

Length of the second string: 5

Example 2 (First string is lexicographically greater):

Input:

Enter the first string: banana

Enter the second string: apple

Output:

The first string is greater than the second string.

Concatenated string: bananaapple

Length of the first string: 6

Length of the second string: 5


7. Implement structures to read, write and compute average- marks of the
students, list the students scoring above and below the average marks for a
class of N students.
/* 7. Implement structures to read, write and compute average- marks of the students, list
the students scoring above and below the average marks for a class of N students*/

#include <stdio.h>

// Define a structure to hold student details

struct Student {

char name[50];

int marks;

};

int main() {

int n;

float average = 0;

// Input the number of students

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

scanf("%d", &n);

struct Student students[n];

// Input student details

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

printf("\nEnter details for student %d:\n", i + 1);

printf("Name: ");

scanf("%s", students[i].name);

printf("Marks: ");

scanf("%d", &students[i].marks);

average += students[i].marks;

// Compute average marks

average /= n;

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


// List students scoring above and below average

printf("\nStudents scoring above average:\n");

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

if (students[i].marks > average) {

printf("%s with marks %d\n", students[i].name, students[i].marks);

printf("\nStudents scoring below average:\n");

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

if (students[i].marks < average) {

printf("%s with marks %d\n", students[i].name, students[i].marks);

return 0;

OUTPUT :

Input:

Enter the number of students: 3

Enter details for student 1:

Name: Alice

Marks: 85

Enter details for student 2:

Name: Bob

Marks: 72

Enter details for student 3:

Name: Charlie

Marks: 90
Processing:

1. Number of students (n) is entered as 3.

2. For each student, their name and marks are entered.

3. The program computes the average marks: average=85+72+903=82.33\text{average}


= \frac{85 + 72 + 90}{3} = 82.33average=385+72+90=82.33

4. Students scoring above and below the average are identified.

Output:

Average marks: 82.33

Students scoring above average:

Alice with marks 85

Charlie with marks 90

Students scoring below average:

Bob with marks 72

Key Notes:

1. Dynamic Array: The program uses variable-length arrays (struct Student students[n])
to store student data.

2. Input Validation: Ensure proper input values; otherwise, unexpected behavior might
occur.

3. Formatting: The average is displayed to two decimal places for clarity.

This example illustrates the flow of input, processing, and output, but you can adjust the input
values to see different results.
8. Write a C program to copy a text file to another, read both the input file
name and target file name.

/* 8. Write a C program to copy a text file to another, read both the input file name and
target file name. */

#include <stdio.h>

#include <stdlib.h>

int main() {

char sourceFile[100], targetFile[100];

FILE *source, *target;

char ch;

// Input source and target file names

printf("Enter the source file name: ");

scanf("%s", sourceFile);

printf("Enter the target file name: ");

scanf("%s", targetFile);

// Open the source file in read mode

source = fopen(sourceFile, "r");

if (source == NULL) {

printf("Error: Unable to open source file %s.\n", sourceFile);

exit(1);

// Open the target file in write mode

target = fopen(targetFile, "w");

if (target == NULL) {
fclose(source);

printf("Error: Unable to create target file %s.\n", targetFile);

exit(1);

// Copy contents from source file to target file

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

fputc(ch, target);

printf("File copied successfully from %s to %s.\n", sourceFile, targetFile);

// Close both files

fclose(source);

fclose(target);

return 0;

Output:

Scenario 1: Successful File Copy

Input:

Enter the source file name: source.txt

Enter the target file name: target.txt

Output (assuming source.txt exists and can be read):

File copied successfully from source.txt to target.txt.

The content of source.txt will now be in target.txt.


Scenario 2: Source File Not Found

Input:

Enter the source file name: non_existing.txt

Enter the target file name: target.txt

Output:

Error: Unable to open source file non_existing.txt.

Scenario 3: Permission Denied for Target File

Input:

Enter the source file name: source.txt

Enter the target file name: /protected/target.txt

Output:

Error: Unable to create target file /protected/target.txt.

Key Points:

 The program performs basic error handling for file operations.

 It overwrites the target file if it already exists.

 Ensure the paths provided for the source and target files are accessible.

You might also like