Aditya 5
Aditya 5
Aditya 5
of
BACHELOR OF TECHNOLOGY
In
Session 2023-24
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
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:
#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:
#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:
#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:
#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:
#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);
#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);
#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");
#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);
#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];
return 0;
}
Output:
#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);