KPR COLLEGE OF ARTS SCIENCE AND RESEARCH
Approved by Government of Tamil Nadu and Affiliated to Bharathiar University, Coimbatore.
Avinashi Road, Arasur, Coimbatore-641 407.
SCHOOL OF COMPUTING SCIENCE
DEPARTMENT OF COMPUTER SCIENCE
PRACTICAL RECORD
PROGRAMMING LAB – C [13P]
BACHELOR OF COMPUTER SCIENCE
ACADEMIC YEAR
2025-2026
ODD SEMESTER
KPR COLLEGE OF ARTS SCIENCE AND RESEARCH
(Affiliated to Bharathiar University)
Register Number:
Name:
Subject:
Subject Code:
CERTIFICATE
Certified that this is a bonafide record of practical work done by the above student during odd semester
of the academic year 2025-2026.
Faculty In-Charge Head of the Department
Submitted for the Bharathiar University Practical Examination held at KPR College of Arts Science and
Research on _________.
Internal Examiner External Examiner
INDEX
PAGE
S.NO DATE CONTENT SIGNATURE
NO
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12
Exercise #1: Find the Sum,Average,Standard Deviation for a given Set
of Numbers
Aim:
Algorithm:
Page |1 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
#include <math.h> // for sqrt function
int main() {
int n, i;
float numbers[100], sum = 0, average, variance = 0, std_deviation;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d numbers:\n", n);
for(i = 0; i < n; i++) {
scanf("%f", &numbers[i]);
sum += numbers[i];
}
average = sum / n;
for(i = 0; i < n; i++) {
variance += (numbers[i] - average) * (numbers[i] - average);
}
variance = variance / n;
std_deviation = sqrt(variance);
printf("\nSum = %.2f", sum);
printf("\nAverage = %.2f", average);
printf("\nStandard Deviation = %.2f\n", std_deviation);
return 0;
}
Output:
Enter the number of elements: 5
Enter 5 numbers:
10
20
30
40
50
Sum = 150.00
Average = 30.00
Standard Deviation = 14.14
Page |2 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #2: Write a C program to generate n prime numbers
Aim:
Algorithm:
Page |3 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
int main() {
int n, count = 0, num = 2, i, isPrime;
printf("Enter how many prime numbers you want: ");
scanf("%d", &n);
printf("First %d Prime Numbers are:\n", n);
while(count < n) {
isPrime = 1; // Assume number is prime
for(i = 2; i <= num/2; i++) {
if(num % i == 0) {
isPrime = 0; // Not a prime number
break;
}
}
if(isPrime) {
printf("%d ", num);
count++;
}
num++;
}
return 0;
}
Output:
Enter how many prime numbers you want: 10
First 10 Prime Numbers are:
2 3 5 7 11 13 17 19 23 29
Page |4 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #3: Write a C program to generate Fibonacci series.
Aim:
Algorithm:
Page |5 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
int main() {
int n, i;
int first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d %d ", first, second);
for(i = 3; i <= n; i++) {
next = first + second;
printf("%d ", next);
first = second;
second = next;
}
return 0;
}
Output:
Enter the number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8
Page |6 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #4: Write a C program to print magic square of order n
where n > 3 and n is odd
Aim:
Algorithm:
Page |7 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the order of magic square (odd number > 3): ");
scanf("%d", &n);
if (n < 3 || n % 2 == 0) {
printf("Only odd numbers greater than 3 are allowed.\n");
return 0;
}
int magic[n][n];
// Initialize matrix with 0
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
magic[i][j] = 0;
int num = 1;
i = 0;
j = n / 2; // Start from middle of first row
while (num <= n * n) {
magic[i][j] = num++;
int newi = (i - 1 + n) % n; // Move up (with wrap around)
int newj = (j + 1) % n; // Move right (with wrap around)
if (magic[newi][newj] != 0) {
i = (i + 1) % n; // Move down if cell already filled
} else {
i = newi;
j = newj;
}
}
// Print the magic square
printf("\nMagic Square of order %d:\n", n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", magic[i][j]);
}
printf("\n");
}
return 0;
}
Page |8 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Output:
Enter the order of magic square (odd number > 3): 5
Magic Square of order 5:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Page |9 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #5: Write a C program to sort the given set of numbers in
ascending order.
Aim:
Algorithm:
P a g e | 10 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d numbers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Numbers in Ascending Order:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter the number of elements: 5
Enter 5 numbers:
25 12 9 30 17
Numbers in Ascending Order:
9 12 17 25 30
P a g e | 11 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #6: Write a C program to check whether the given string is
a palindrome or not using pointers.
Aim:
Algorithm:
P a g e | 12 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
char *left, *right;
int isPalindrome = 1; // Assume it is a palindrome initially
printf("Enter a string: ");
scanf("%s", str);
left = str; // Pointer to first character
right = str + strlen(str) - 1; // Pointer to last character
while (left < right) {
if (*left != *right) {
isPalindrome = 0; // Not a palindrome
break;
}
left++;
right--;
}
if (isPalindrome)
printf("The string is a Palindrome.\n");
else
printf("The string is NOT a Palindrome.\n");
return 0;
}
Output:
Enter a string: madam
The string is a Palindrome.
P a g e | 13 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #7: Write a C program to count the number of Vowels in
the given sentence.
Aim:
Algorithm:
P a g e | 14 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
#include <ctype.h> // For tolower() function
int main() {
char str[1000];
int count = 0;
int i;
// Input sentence from user
printf("Enter a sentence: ");
fgets(str, sizeof(str), stdin); // to read the entire line with spaces
// Loop through each character of the string
for(i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]); // Convert to lowercase for case-insensitivity
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++; // Increment count if it's a vowel
// Output the result
printf("The number of vowels in the sentence: %d\n", count);
return 0;
P a g e | 15 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Output:
Enter a sentence: Hello, how are you?
The number of vowels in the sentence: 7
P a g e | 16 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #8: Write a C program to find the factorial of a given
number using recursive function.
Aim:
Algorithm:
P a g e | 17 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
// Function to calculate factorial recursively
int factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n == 0 || n == 1) {
return 1;
}
// Recursive case
else {
return n * factorial(n - 1);
}
}
int main() {
int num;
// Input number
printf("Enter a number: ");
scanf("%d", &num);
// Calling the recursive function
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is %d\n", num, factorial(num));
}
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is 120
P a g e | 18 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #9 : Write a C program to print the students Mark sheet
assuming roll no, name, and marks in 5 subjects in a structure.
Create an array of structures and print the mark sheet in the
university pattern.
Aim:
Algorithm:
P a g e | 19 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
struct Student {
int roll_no;
char name[50];
int marks[5];
int total;
float percentage;
};
// Function to calculate total and percentage
void calculate(struct Student* s) {
s->total = 0;
for(int i = 0; i < 5; i++) {
s->total += s->marks[i];
s->percentage = (float)s->total / 5;
int main() {
int n;
// Input number of students
printf("Enter number of students: ");
scanf("%d", &n);
P a g e | 20 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
struct Student students[n]; // Array of structures
// Input details for each student
for(int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].roll_no);
getchar(); // To consume the newline character left by scanf
printf("Name: ");
fgets(students[i].name, 50, stdin); // Input name with spaces
printf("Enter marks in 5 subjects:\n");
for(int j = 0; j < 5; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &students[i].marks[j]);
calculate(&students[i]);
// Printing the mark sheet
printf("\nMark Sheet:\n");
printf("-----------------------------------------------------------\n");
printf("Roll No | Name | Subject 1 | Subject 2 | Subject 3 | Subject 4 |
Subject 5 | Total | Percentage\n");
printf("-----------------------------------------------------------\n");
for(int i = 0; i < n; i++) {
P a g e | 21 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
printf("%-8d| %-30s", students[i].roll_no, students[i].name);
for(int j = 0; j < 5; j++) {
printf("%-10d", students[i].marks[j]);
printf("| %-5d | %-10.2f\n", students[i].total, students[i].percentage);
return 0;
Output:
Enter number of students: 2
Enter details for student 1:
Roll Number: 101
Name: John Doe
Enter marks in 5 subjects:
Subject 1: 85
Subject 2: 90
Subject 3: 88
Subject 4: 92
Subject 5: 87
Enter details for student 2:
Roll Number: 102
Name: Jane Smith
Enter marks in 5 subjects:
Subject 1: 80
Subject 2: 85
Subject 3: 88
Subject 4: 75
Subject 5: 90
Mark Sheet:
-----------------------------------------------------------
Roll No | Name | Subject 1 | Subject 2 | Subject 3 | Subject 4 | Subject 5 | Total | Percentage
-----------------------------------------------------------
101 | John Doe | 85 90 88 92 87 | 442 | 88.40
102 | Jane Smith | 80 85 88 75 90 | 418 | 83.60
P a g e | 22 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #10: Write a function using pointers to add two matrices and
to return the resultant matrix to the calling function.
Aim:
Algorithm:
P a g e | 23 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
// Function to add two matrices using pointers and return the resultant matrix
void add_matrices(int *A, int *B, int *result, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// Calculate the index for the 2D array (linearized as 1D array)
*(result + i * cols + j) = *(A + i * cols + j) + *(B + i * cols + j);
int main() {
int rows, cols;
// Input number of rows and columns for the matrices
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
// Declare the matrices as 1D arrays (using dynamic memory allocation)
int A[rows][cols], B[rows][cols], result[rows][cols];
// Input elements of the first matrix A
printf("Enter elements of matrix A:\n");
P a g e | 24 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
// Input elements of the second matrix B
printf("Enter elements of matrix B:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("B[%d][%d]: ", i, j);
scanf("%d", &B[i][j]);
// Call the function to add matrices A and B
add_matrices((int *)A, (int *)B, (int *)result, rows, cols);
// Output the resultant matrix
printf("\nResultant Matrix (A + B):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
printf("\n");
P a g e | 25 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
}
return 0;
Output:
Enter number of rows: 2
Enter number of columns: 3
Enter elements of matrix A:
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
Enter elements of matrix B:
B[0][0]: 6
B[0][1]: 5
B[0][2]: 4
B[1][0]: 3
B[1][1]: 2
B[1][2]: 1
Resultant Matrix (A + B):
777
777
P a g e | 26 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #11: Write a C program which receives two filenames as
arguments and check whether the file contents are same or not. If
same delete the second file
Aim:
Algorithm:
P a g e | 27 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding :
#include <stdio.h>
#include <stdlib.h>
int compare_files(FILE *file1, FILE *file2) {
char ch1, ch2;
// Compare the files byte by byte
while ((ch1 = fgetc(file1)) != EOF && (ch2 = fgetc(file2)) != EOF) {
if (ch1 != ch2) {
return 0; // Files are not the same
// If one file ends earlier than the other, they are not the same
if ((ch1 != EOF) || (ch2 != EOF)) {
return 0;
return 1; // Files are the same
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <file1> <file2>\n", argv[0]);
return 1;
P a g e | 28 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
FILE *file1 = fopen(argv[1], "r");
FILE *file2 = fopen(argv[2], "r");
// Check if both files are opened successfully
if (file1 == NULL) {
printf("Error: Could not open file %s\n", argv[1]);
return 1;
if (file2 == NULL) {
printf("Error: Could not open file %s\n", argv[2]);
fclose(file1);
return 1;
// Compare files
if (compare_files(file1, file2)) {
printf("The files are identical.\n");
// Delete the second file
if (remove(argv[2]) == 0) {
printf("The second file %s has been deleted.\n", argv[2]);
} else {
printf("Error: Could not delete file %s\n", argv[2]);
} else {
printf("The files are different.\n");
P a g e | 29 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
}
// Close the files
fclose(file1);
fclose(file2);
return 0;
Output:
$ ./file_compare file1.txt file2.txt
The files are identical.
The second file file2.txt has been deleted.
P a g e | 30 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Exercise #12: Write a program which takes a file as command line
argument and copy it to another file. At the end of the second file
write the total i) no of chars ii) no. of words and iii) no. of lines
Aim:
Algorithm:
P a g e | 31 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
Coding:
#include <stdio.h>
#include <ctype.h>
void copy_file_and_count(FILE *source, FILE *destination) {
char ch;
int char_count = 0, word_count = 0, line_count = 0;
int in_word = 0;
// Copy contents from source to destination while counting characters, words, and lines
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination); // Copy character to destination
char_count++; // Count each character
// Count words (transition from non-space to space or newline)
if (isspace(ch)) {
if (in_word) {
word_count++; // End of a word
in_word = 0;
}
if (ch == '\n') {
line_count++; // Count new line
}
} else {
in_word = 1; // In the middle of a word
}
}
// If the last word is not followed by a space or newline, count it
if (in_word) {
word_count++;
}
P a g e | 32 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
// Handle the case where the last line does not end with a newline
if (ch == EOF && char_count > 0 && ch != '\n') {
line_count++;
}
// Append total counts at the end of the destination file
fprintf(destination, "\n\nTotal counts:\n");
fprintf(destination, "Characters: %d\n", char_count);
fprintf(destination, "Words: %d\n", word_count);
fprintf(destination, "Lines: %d\n", line_count);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <source_file> <destination_file>\n", argv[0]);
return 1;
}
// Open source file in read mode
FILE *source = fopen(argv[1], "r");
if (source == NULL) {
printf("Error: Could not open source file %s\n", argv[1]);
return 1;
}
// Open destination file in write mode
FILE *destination = fopen(argv[2], "w");
if (destination == NULL) {
printf("Error: Could not open destination file %s\n", argv[2]);
fclose(source);
return 1;
}
// Copy the file and count characters, words, and lines
copy_file_and_count(source, destination);
P a g e | 33 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore
printf("File copied and statistics added successfully.\n");
// Close both files
fclose(source);
fclose(destination);
return 0;
}
Output:
Hello World!
This is a test.
File copying example.
P a g e | 34 Dept. of CS/ KPR College of Arts Science and Research/ Coimbatore