IntegratedLabManual1 1 2025
IntegratedLabManual1 1 2025
COLLEGE OF ENGINEERING
(An Autonomous Institution Affiliated to VTU, Belagavi)
I SEMESTER
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.
#include <stdio.h>
int main() {
char operator;
// Display menu
printf("Simple Calculator\n");
// Input numbers
// Perform calculation
switch (operator) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
if (num2 != 0) {
} else {
break;
default:
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
Subtraction Example: If the user enters - for the operator and inputs 10 and 4 as the two
numbers, the output will be:
Simple Calculator
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 (+, -, *, /): *
Division Example: If the user enters / for the operator and inputs 8 and 4 as the two
numbers, the output will be:
Simple Calculator
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
Invalid Operator Example: If the user enters an invalid operator, such as #, the output will
be:
Simple Calculator
#include <stdio.h>
if (arr[mid] == target)
return mid;
low = mid + 1;
high = mid - 1;
return -1;
int main() {
int target;
scanf("%d", &target);
if (result != -1)
else
return 0;
Output:
Explanation: The target 10 is at index 3 in the sorted array {2, 3, 4, 10, 40}
#include <stdio.h>
arr[j + 1] = temp;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
Output :
Enter 5 numbers:
53842
Step-by-Step Execution:
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>
void multiplyMatrices(int row1, int col1, int mat1[row1][col1], int row2, int col2, int
mat2[row2][col2], int result[row1][col2]) {
int main() {
printf("Enter the number of rows and columns of the first matrix: ");
printf("Enter the number of rows and columns of the second matrix: ");
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];
scanf("%d", &mat1[i][j]);
scanf("%d", &mat2[i][j]);
// Multiply matrices
printf("\n");
return 0;
}
Output :
Let's consider an example where the user provides the following input:
Matrix 1: 2x3 matrix:
123
456
78
9 10
11 12
Input:
123
456
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):
58 64
139 154
#include <stdio.h>
#include <string.h>
int main() {
char name[50];
int units;
scanf("%d", &units);
} else {
}
// Add minimum meter charge
totalAmount = 100;
totalAmount += surcharge;
printf("\nElectricity Bill\n");
printf("-------------------------------\n");
return 0;
Output :
Input:
Output:
Electricity Bill
-------------------------------
User Name : John Doe
Surcharge : Rs 0.00
Explanation:
Since the units are less than 200, the charge is Rs 0.80 per unit.
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.
#include <stdio.h>
#include <string.h>
if (*str1 != *str2) {
str1++;
str2++;
while (*dest) {
dest++;
while (*src) {
*dest = *src;
dest++;
src++;
*dest = '\0';
}
// Function to find the length of a string
int length = 0;
while (*str) {
length++;
str++;
return length;
int main() {
// Input strings
// String comparison
if (cmpResult == 0) {
} else {
// String concatenation
strcpy(result, str1); // Copy first string to result
// String lengths
return 0;
Output:
Input:
Output:
Input:
Output:
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
int n;
float average = 0;
scanf("%d", &n);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%d", &students[i].marks);
average += students[i].marks;
average /= n;
return 0;
OUTPUT :
Input:
Name: Alice
Marks: 85
Name: Bob
Marks: 72
Name: Charlie
Marks: 90
Processing:
Output:
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.
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 ch;
scanf("%s", sourceFile);
scanf("%s", targetFile);
if (source == NULL) {
exit(1);
if (target == NULL) {
fclose(source);
exit(1);
fputc(ch, target);
fclose(source);
fclose(target);
return 0;
Output:
Input:
Input:
Output:
Input:
Output:
Key Points:
Ensure the paths provided for the source and target files are accessible.