Aditya 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Lab Record

of

Programming for Problem Solving


(CSF101)

BACHELOR OF TECHNOLOGY
In

COMPUTER SCIENCE AND ENGINEERING

Session 2023-24

Submitted to: - Submitted by: -


Sumita Lamba Name: Aditya Kumar
Assistant Professor Roll No.: 230102198
School of Computing Sap ID: 1000019772
DIT University, Dehradun Class/Section: I

SCHOOL OF COMPUTING
DIT UNIVERSITY, DEHRADUN
(State Private University through State Legislature Act No. 10 of 2013 of Uttarakhand and approved by UGC)
Mussoorie Diversion Road, Dehradun, Uttarakhand - 248009, India.

August-December 2023
INDEX

INDEX
CONDUCTION INSTRUCTOR’s
S.NO. NAME OF THE EXPERIMENTS
DATE SIGNATURE
Write a C program to print the value and the
1
address of a variable
Write a C program to swap two elements using
2
pointer
Write a C program to calculate the sum of two
3
numbers using pointer
Write a C program to calculate the sum of two
4
numbers of an array using a pointer
Write a C program to swap two variables using
5
reference method
6 Write a C program to write a string in a file
A file name data contains a series of integer
numbers. Write a C program to read all numbers
from the file, then write all the odd numbers into a
7
file name “odd” and write all the even numbers
into an “even”, Display all the contents of this files
on the screen
Write a C program to read the names and marks
8
of n number of students and store them in a file
Write a C program to print contents in the reverse
9
order of a file
Write a C program to compare the contents of two
10
files
WAP to copy several bytes from a specific offset to
11
another file
WAP to convert all the characters in the UPPER
12
CASE of a File.
Practical-01

Objective: Write a C program to print the value and address of a variable.

Code:

#include <stdio.h>
void main() {
int myVariable = 42;
printf("Value of myVariable: %d\n", myVariable);
printf("Address of myVariable: %p\n", (void*)&myVariable);
}
Output:
Practical-02
Objective: Write a C program to swap two elements using a pointer.
Code:

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void main() {
int x,y;
printf("Enter 2 numbers: ");
scanf("%i%i", &x,&y);
printf("Before swapping: x=%i, y=%i\n", x, y);
swap(&x, &y);
printf("After swapping: x=%i, y=%i\n", x, y);
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-03
Objective: Write a C program to calculate the sum of two numbers using a pointer.
Code:

#include <stdio.h>
void Sum(int *a, int *b, int *sum){
*sum = *a + *b;}
int main() {
int n1, n2, s;
printf("Enter the first number: ");
scanf("%d", &n1);
printf("Enter the second number: ");
scanf("%d", &n2);
Sum(&n1, &n2, &s);
printf("Sum: %i+%i=%i\n",n1,n2,s);
}
Output:

Aditya 230102198 B. Tech (1st Semester, Section-I)


Kumar School of Computing
Practical-04
Objective: Write a C program to calculate the sum ofelements of an array using a
pointer.
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;}
void main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr)/sizeof(arr[0]);
int sum = calculateSum(arr, size);
printf("Sum of array elements:%d\n",sum);
}
Output:

Aditya Kuma 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-05
Objective: Write a C program to swap the value of two variables using the reference
method.
Code:

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void main() {
int x,y;
printf("Enter 2 numbers: ");
scanf("%i%i", &x,&y);
printf("Before swapping: x=%i, y=%i\n", x, y);
swap(&x, &y);
printf("After swapping: x=%i, y=%i\n", x, y);
}
Output:

Aditya Kuma 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-06
Objective: Write a C program to write a string in a file
Code:

#include <stdio.h>
int main() {
FILE *file;
char fileName[50], myString[100];
printf("Enter the filename: ");
scanf("%s", fileName);
printf("Enter the string to be written to the file: ");
scanf(" %[^\n]s", myString);
file = fopen(fileName, "w");
if (file == NULL) {
fprintf(stderr, "Error opening file '%s' for writing.\n", fileName);
return 1; }
fprintf(file, "%s", myString);
fclose(file);
printf("String successfully written to the file '%s'.\n", fileName);
return 0;}
Output:

Aditya Kuma 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-07
Objective: A file named data contains a series of integer numbers. Write a C program to
readall numbers from the file, then write all the odd numbers into file named “odd”
and write all even numbers into an “even.” Display all the contents of these files on the
screen.
Code:

#include <stdio.h>
int main() {
FILE *dataFile, *oddFile, *evenFile;
int number;
dataFile = fopen("data.txt", "r");
if (dataFile == NULL) {
printf("Error opening data file.\n");
return 1;}
oddFile = fopen("odd.txt", "w");
if (oddFile == NULL) {
printf("Error opening odd file.\n");
fclose(dataFile);
return 1;}
evenFile = fopen("even.txt", "w");
if (evenFile == NULL) {
printf("Error opening even file.\n");
fclose(dataFile);
fclose(oddFile);
return 1;}
while (fscanf(dataFile, "%d", &number) == 1) {
if (number % 2 == 0) {
fprintf(evenFile, "%d\n", number);

Sonali Yadav 230102023 B. Tech (1st Semester, Section-I)


School of Computing
} else {
fprintf(oddFile, "%d\n", number);}}
fclose(dataFile);
fclose(oddFile);
fclose(evenFile);
printf("Contents of odd file:\n");
oddFile = fopen("odd.txt", "r");
while (fscanf(oddFile, "%d", &number) == 1) {
printf("%d\n", number);}
fclose(oddFile);
printf("\nContents of even file:\n");
evenFile = fopen("even.txt", "r");
while (fscanf(evenFile, "%d", &number) == 1) {
printf("%d \n", number);}
fclose(evenFile);
return 0;}
Output:-

Aditya kuma 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-08
Objective: Write a C program to read the names andmarks of n number of students and
store them in a file.
Code:

#include <stdio.h>
int main() {
FILE *file;
char fileName[50];
int n;
printf("Enter the filename: ");
scanf("%s", fileName);
file = fopen(fileName, "w");
if (file == NULL) {
fprintf(stderr, "Error opening file '%s' for writing.\n", fileName);
return 1;}
printf("Enter the number of students: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
char name[50];
float marks;
printf("Enter the name of student %d: ", i + 1);
scanf("%s", name);
printf("Enter the marks of student %d: ", i + 1);
scanf("%f", &marks);
fprintf(file, "%s %.2f\n", name, marks);}
fclose(file);

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
printf("Names and marks of %d students successfully stored in '%s'.\n", n,
fileName);
return 0;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-09
Objective: Write a C program to print contents in the reverse order of a file.
Code:

#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char fileName[50];
char ch;
long fileSize;
printf("Enter the filename: ");
scanf("%s", fileName);
file = fopen(fileName, "r");
if (file == NULL) {
fprintf(stderr, "Error opening file '%s' for reading.\n", fileName);
return 1;
}
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
for (long i = fileSize - 1; i >= 0; i--) {
fseek(file, i, SEEK_SET);
ch = fgetc(file);
printf("%c", ch);
}
fclose(file);
printf("\nFile content reversed.\n");

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
return 0;}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-10
Objective: Write a C program to compare the contentsof two files.
Code:

#include <stdio.h>
#include <stdlib.h>
int compareFiles(FILE *file1, FILE *file2);
int main() {
FILE *file1, *file2;
char fileName1[50], fileName2[50];
printf("Enter the filename for the first file: ");
scanf("%s", fileName1);

printf("Enter the filename for the second file: ");


scanf("%s", fileName2);
file1 = fopen(fileName1, "r");
file2 = fopen(fileName2, "r");
if (file1 == NULL || file2 == NULL) {
fprintf(stderr, "Error opening files for reading.\n");
return 1;
}
if (compareFiles(file1, file2)) {
printf("The contents of the files are identical.\n");
} else {
printf("The contents of the files are different.\n");
}
fclose(file1);

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
fclose(file2);
return 0;
}

int compareFiles(FILE *file1, FILE *file2) {


char ch1, ch2;
while ((ch1 = fgetc(file1)) != EOF && (ch2 = fgetc(file2)) != EOF) {
if (ch1 != ch2) {
return 0;
}
}
if (ch1 == EOF && ch2 == EOF) {
return 1;
} else {
return 0;
}
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-11
Objective: Write a C program to copy several bytesfrom a specific offset to another file.
Code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *sourceFile, *destinationFile;
char sourceFileName[100], destinationFileName[100];
long offset, bytesToCopy;
int bytesRead, bytesWritten;
char buffer[1024];

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


scanf("%s", sourceFileName);

sourceFile = fopen(sourceFileName, "rb");


if (sourceFile == NULL) {
perror("Error opening source file");
return 1;
}

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


scanf("%s", destinationFileName);

destinationFile = fopen(destinationFileName, "wb");

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
if (destinationFile == NULL) {
perror("Error opening destination file");
fclose(sourceFile);
return 1;
}

printf("Enter the offset (in bytes): ");


scanf("%ld", &offset);

printf("Enter the number of bytes to copy: ");


scanf("%ld", &bytesToCopy);
fseek(sourceFile, offset, SEEK_SET);
while (bytesToCopy > 0) {
bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile);
if (bytesRead <= 0) {
break; // End of file or error
}

bytesWritten = fwrite(buffer, 1, bytesRead, destinationFile);


if (bytesWritten <= 0) {
perror("Error writing to destination file");
fclose(sourceFile);
fclose(destinationFile);
return 1;
}
bytesToCopy -= bytesWritten;

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
}
fclose(sourceFile);
fclose(destinationFile);

printf("File copy successful!\n");

return 0;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
Practical-12
Objective: Write a C program to convert all charactersin the UPPER CASE of a File
Code:

#include <stdio.h>
#include <ctype.h>
int main() {
FILE *inputFile, *outputFile;
char inputFileName[50], outputFileName[50];
char ch;
printf("Enter the input filename: ");
scanf("%s", inputFileName);
inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
fprintf(stderr, "Error opening input file '%s' for reading.\n",
inputFileName);
return 1;}
printf("Enter the output filename: ");
scanf("%s", outputFileName);
outputFile = fopen(outputFileName, "w");
if (outputFile == NULL) {
fprintf(stderr, "Error opening output file '%s' for writing.\n",
outputFileName);
fclose(inputFile);
return 1;
}
while ((ch = fgetc(inputFile)) != EOF) {
ch = (char)toupper(ch);

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing
fputc(ch, outputFile);
}
fclose(inputFile);
fclose(outputFile);
printf("File successfully converted to uppercase. Check '%s'.\n",
outputFileName);
return 0;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-I)


School of Computing

You might also like