0% found this document useful (0 votes)
242 views23 pages

Aditya

The document contains a list of 12 programming assignments for practicing C programming concepts like variables, pointers, arrays, files, strings, etc. Each assignment contains the objective, code snippet, and sample output. The assignments involve writing programs to print variable values and addresses, swap values using pointers, calculate sums using pointers, read/write to files, reverse a file's contents, and other basic file and string manipulation tasks.

Uploaded by

saksham arora
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)
242 views23 pages

Aditya

The document contains a list of 12 programming assignments for practicing C programming concepts like variables, pointers, arrays, files, strings, etc. Each assignment contains the objective, code snippet, and sample output. The assignments involve writing programs to print variable values and addresses, swap values using pointers, calculate sums using pointers, read/write to files, reverse a file's contents, and other basic file and string manipulation tasks.

Uploaded by

saksham arora
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/ 23

INDEX

S.No Title of Experiment Date Signature


1. Write a C program to print the value and address of a 13/11/2023
variable.
2. Write a C program to swap two elements using a 13/11/2023
pointer
3. Write a C program to Calculate the sum of two 13/11/2023
numbers using a pointer
4. Write a C program to calculate the sum of element of 13/11/2023
an array using a pointer.
5. Write a C program to swap the value of two variable 13/11/2023
using the reference method.
6. Write a C program to write a string in a file. 13/11/2023
7. A File named data consist of a series of integer 13/11/2023
numbers. Write a C program to read all numbers from
the file, then write all the odd numbers into a file
named “odd” and write all even numbers into an
“even”. Display all the contents of these files on the
screen.
8. Write a C program to read the names and marks of n 13/11/2023
numbers of students and store them in a file.
9. Write a C program to print contents in the reverse 13/11/2023
order of a file.
10. Write a C program to compare the contents of two file 13/11/2023
11. Write a C program to copy several bytes a specific 13/11/2023
offset to another file.
12. Write a C program to convert all character in the 13/11/2023
UPPERCASE of a File.

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to print the value and address of a
variable.
CODE: -
#include <stdio.h>
#include<conio.h>
int main() {
int myVariable;
myVariable = 42;
clrscr();
printf("Value of myVariable: %d\n", myVariable);
printf("Address of myVariable: %p\n", (void*)&myVariable);
getch();
return 0;
}

OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to swap two elements
using a pointer.
CODE: -
#include <stdio.h>
#include<conio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
clrscr();
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Before swapping: \n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
swap(&num1, &num2);
printf("\nAfter swapping: \n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
getch();
return 0;
}

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OUTPUT:-

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to Calculate the sum of
two numbers using a pointer.
CODE: -
#include <stdio.h>
#include<conio.h>
void add(int *a, int *b, int *result) {
*result = *a + *b;
}
int main() {
int num1, num2, sum;
clrscr();
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
add(&num1, &num2, &sum);
printf("Sum of %d and %d is: %d\n", num1, num2, sum);
getch();
return 0;
}

OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to calculate the sum of
element of an array using a pointer.
CODE: -
#include <stdio.h>
#include<conio.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 size;
clrscr();
printf("Enter the size of the array: ");
scanf("%d", &size);
int array[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}
int sum = calculateSum(array, size);
printf("Sum of elements in the array is: %d\n", sum);
getch();
return 0;
}

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to swap the value of two
variable using the reference method.
CODE: -
#include <stdio.h>
#include<conio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
clrscr();
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Before swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
swap(&num1, &num2);
printf("\nAfter swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
getch();
return 0;
}

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to write a string in a file.
CODE: -
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
int main() {
FILE *filePointer;
char fileName[50];
char content[100];
clrscr();
printf("Enter the filename: ");
scanf("%s", fileName);
filePointer = fopen(fileName, "w");
if (filePointer == NULL) {
printf("Error opening the file.\n");
return 1;
}
printf("Enter the string to be written to the file: ");
getchar();
fgets(content, sizeof(content), stdin);
fprintf(filePointer, "%s", content);
fclose(filePointer);
printf("File \"%s\" has been written successfully.\n", fileName);
getch();
return 0;
}

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - A File named data consist of a series of
integer numbers. Write a C program to read all numbers
from the file, then write all the odd numbers into a 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>
#include<conio.h>
int main() {
FILE *inputFile, *oddFile, *evenFile;
int number;
clrscr();
inputFile = fopen("data.txt", "r");
if (inputFile == NULL) {
printf("Error opening the input file.\n");
return 1;
}
oddFile = fopen("odd.txt", "w");
evenFile = fopen("even.txt", "w");
if (oddFile == NULL || evenFile == NULL) {
printf("Error opening the output files.\n");
fclose(inputFile);
return 1;
}
while (fscanf(inputFile, "%d", &number) == 1) {
if (number % 2 == 0) {
fprintf(evenFile, "%d\n", number);
} else {

Name: - Aditya Kumar Section : - I Roll No: - 230102198


fprintf(oddFile, "%d\n", number);
}
}
fclose(inputFile);
fclose(oddFile);
fclose(evenFile);
printf("Contents of odd.txt:\n");
oddFile = fopen("odd.txt", "r");
while (fscanf(oddFile, "%d", &number) == 1) {
printf("%d\n", number);
}
fclose(oddFile);
printf("\nContents of even.txt:\n");
evenFile = fopen("even.txt", "r");
while (fscanf(evenFile, "%d", &number) == 1) {
printf("%d\n", number);
}
fclose(evenFile);
getch();
return 0;
}

OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to read the names and
marks of n numbers of students and store them in a file.

CODE: -
#include <stdio.h>
#include<conio.h>
int main() {
FILE *filePointer;
char fileName[50];
int n;
printf("Enter the filename: ");
scanf("%s", fileName);
filePointer = fopen(fileName, "w");
if (filePointer == NULL) {
printf("Error opening the file.\n");
return 1; // Return with an error code
}
printf("Enter the number of students: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
char name[50];
int marks;
printf("Enter the name of student %d: ", i + 1);
scanf("%s", name);
printf("Enter the marks of student %d: ", i + 1);
scanf("%d", &marks);
fprintf(filePointer, "%s %d\n", name, marks);
}
fclose(filePointer);

Name: - Aditya Kumar Section : - I Roll No: - 230102198


printf("Data has been written to the file \"%s\" successfully.\n", fileName);
getch();
return 0;
}

>OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to print contents in the
reverse order of a file.
CODE: -
#include <stdio.h>
#include<conio.h>
int main() {
FILE *filePointer;
char fileName[50];
char line[100];
printf("Enter the filename: ");
scanf("%s", fileName);
filePointer = fopen(fileName, "r");
if (filePointer == NULL) {
printf("Error opening the file.\n");
return 1; // Return with an error code
}
char lines[100][100];
int lineCount = 0;
while (fgets(line, sizeof(line), filePointer) != NULL) {
sscanf(line, "%99[^\n]", lines[lineCount]);
lineCount++;
}
printf("Contents of the file in reverse order:\n");
for (int i = lineCount - 1; i >= 0; i--) {
printf("%s\n", lines[i]);
}
fclose(filePointer);
getch();
return 0;}

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to compare the contents
of two file.
CODE: -
#include <stdio.h>
#include <string.h>
#include<conio.h>
#define MAX_SIZE 100
int compareFiles(FILE *file1, FILE *file2) {
char line1[MAX_SIZE], line2[MAX_SIZE];
int lineNum = 1;
while (fgets(line1, sizeof(line1), file1) != NULL && fgets(line2, sizeof(line2), file2) != NULL) {
if (strcmp(line1, line2) != 0) {
printf("Files differ at line %d\n", lineNum);
return 0;
}
lineNum++;
}
if (fgets(line1, sizeof(line1), file1) != NULL || fgets(line2, sizeof(line2), file2) != NULL) {
printf("Files have different lengths\n");
return 0;
}

return 1;
}

int main() {
FILE *file1, *file2;
char fileName1[50], fileName2[50];
printf("Enter the first filename: ");

Name: - Aditya Kumar Section : - I Roll No: - 230102198


scanf("%s", fileName1);
printf("Enter the second filename: ");
scanf("%s", fileName2);
file1 = fopen(fileName1, "r");
file2 = fopen(fileName2, "r");
if (file1 == NULL || file2 == NULL) {
printf("Error opening the files.\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);
fclose(file2);
return 0;
}

OUTPUT: -
File1.txt

File2.txt

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to copy several bytes a
specific offset to another file.

CODE: -
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
void copyBytes(FILE *source, FILE *destination, long offset, size_t numBytes) {
fseek(source, offset, SEEK_SET);
char *buffer = (char *)malloc(numBytes);
if (buffer == NULL) {
printf("Memory allocation failed.\n");
exit(1); // Exit with an error code
}
size_t bytesRead = fread(buffer, 1, numBytes, source);
fwrite(buffer, 1, bytesRead, destination);
free(buffer);
}
int main() {
FILE *sourceFile, *destinationFile;
char sourceFileName[50], destinationFileName[50];
long offset;
size_t numBytes;
clrscr();
printf("Enter the source filename: ");
scanf("%s", sourceFileName);
sourceFile = fopen(sourceFileName, "rb");
if (sourceFile == NULL) {

Name: - Aditya Kumar Section : - I Roll No: - 230102198


printf("Error opening the source file.\n");
return 1;
}
printf("Enter the destination filename: ");
scanf("%s", destinationFileName);
destinationFile = fopen(destinationFileName, "wb");
if (destinationFile == NULL) {
fclose(sourceFile);
printf("Error opening the destination file.\n");
return 1;
}
printf("Enter the offset: ");
scanf("%ld", &offset);
printf("Enter the number of bytes to copy: ");
scanf("%zu", &numBytes);
copyBytes(sourceFile, destinationFile, offset, numBytes);
fclose(sourceFile);
fclose(destinationFile);
printf("Bytes copied successfully from %s to %s.\n", sourceFileName, destinationFileName);
getch();
return 0;
}

OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198


OBJECTIVE: - Write a C program to convert all character
in the UPPERCASE of a File.
CODE: -
#include <stdio.h>
#include <ctype.h>
#include<conio.h>
int main() {
FILE *inputFile, *outputFile;
char inputFileName[50], outputFileName[50];
char ch;
clrscr();
printf("Enter the source filename: ");
scanf("%s", inputFileName);
inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
printf("Error opening the source file.\n");
return 1;
}
printf("Enter the destination filename: ");
scanf("%s", outputFileName);
outputFile = fopen(outputFileName, "w");
if (outputFile == NULL) {
fclose(inputFile);
printf("Error opening the destination file.\n");
return 1;
}
while ((ch = fgetc(inputFile)) != EOF) {
fputc(toupper(ch), outputFile);
}

Name: - Aditya Kumar Section : - I Roll No: - 230102198


fclose(inputFile);
fclose(outputFile);
printf("Conversion to UPPERCASE completed successfully from %s to %s.\n", inputFileName, outputFileName);
getch();
return 0;
}

OUTPUT: -

Name: - Aditya Kumar Section : - I Roll No: - 230102198

You might also like