BCSL 021(EACH QUESTION 20 MARKS)
1. Write a C' program to find the second largest number among 3 numbers given as input.
#include <stdio.h>
int main() {
int a, b, c;
// Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
int second;
// Logic to find the second largest
if ((a > b && a < c) || (a > c && a < b))
second = a;
else if ((b > a && b < c) || (b > c && b < a))
second = b;
else
second = c;
// Output result
printf("The second largest number is: %d\n", second);
return 0;
}
OUTPUT
Enter three numbers: 10 25 15
The second largest number is: 15
2. Using structures, write an interactive `C program to find the total marks, average marks
in the first semester courses of BCA for 5 students of your class.. 20 Note :
Assumptions can be made wherever necessary.
#include <stdio.h>
#include <string.h>
#define NUM_STUDENTS 5
#define NUM_SUBJECTS 4
// Define structure for a student
struct Student {
char name[50];
int marks[NUM_SUBJECTS];
int total;
float average;
};
int main() {
struct Student students[NUM_STUDENTS];
int i, j;
printf("Enter details for %d BCA students:\n", NUM_STUDENTS);
for (i = 0; i < NUM_STUDENTS; i++) {
printf("\nStudent %d\n", i + 1);
// Input student name
printf("Enter name: ");
scanf(" %[^\n]", students[i].name); // Reads string with spaces
// Input marks for 4 subjects
students[i].total = 0;
for (j = 0; j < NUM_SUBJECTS; j++) {
printf("Enter marks for Subject %d: ", j + 1);
scanf("%d", &students[i].marks[j]);
students[i].total += students[i].marks[j];
}
// Calculate average
students[i].average = students[i].total / (float)NUM_SUBJECTS;
}
// Display results
printf("\n--- Student Results ---\n");
printf("%-20s %-10s %-10s\n", "Name", "Total", "Average");
for (i = 0; i < NUM_STUDENTS; i++) {
printf("%-20s %-10d %-10.2f\n",
students[i].name,
students[i].total,
students[i].average);
}
return 0;
}
OUTPUT
Enter details for 5 BCA students:
Student 1
Enter name: Arjun Sharma
Enter marks for Subject 1: 78
Enter marks for Subject 2: 85
Enter marks for Subject 3: 92
Enter marks for Subject 4: 88
... (repeat for others)
--- Student Results ---
Name Total Average
Arjun Sharma 343 85.75
...
3. Write a C program to add 2 matrices A (2 X 2), B (2 x 2) and store the sum in matrix C.
#include <stdio.h>
int main() {
int A[2][2], B[2][2], C[2][2];
int i, j;
// Input matrix A
printf("Enter elements of matrix A (2x2):\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
// Input matrix B
printf("\nEnter elements of matrix B (2x2):\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("B[%d][%d]: ", i, j);
scanf("%d", &B[i][j]);
}
}
// Add A and B and store in C
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// Display result
printf("\nSum of matrices A and B (Matrix C):\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
Enter elements of matrix A (2x2):
A[0][0]: 1
A[0][1]: 2
A[1][0]: 3
A[1][1]: 4
Enter elements of matrix B (2x2):
B[0][0]: 5
B[0][1]: 6
B[1][0]: 7
B[1][1]: 8
Sum of matrices A and B (Matrix C):
6 8
10 12
4. Write an interactive C program to remove the duplicates in a given word (string) and
display the individual characters with a comma delimiter. Example : I/P = CALCULATE
and O/P= C,A,L,C,U,L,A,T,E
#include <stdio.h>
#include <string.h>
int main() {
char input[100];
int i, j, k;
int len;
printf("Enter a word: ");
scanf("%s", input); // Read the word
len = strlen(input);
// Remove duplicates
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; ) {
if (input[i] == input[j]) {
// Shift all characters to the left
for (k = j; k < len - 1; k++) {
input[k] = input[k + 1];
}
input[len - 1] = '\0';
len--;
} else {
j++;
}
}
}
// Print with comma delimiter
printf("Output: ");
for (i = 0; i < len; i++) {
printf("%c", input[i]);
if (i != len - 1) {
printf(",");
}
}
printf("\n");
return 0;
}
OUTPUT
Enter a word: CALCULATE
Output: C,A,L,U,T,E
5. Write a C program to calculate the perimeter and area of a rectangle whose length and
breadth are given. 20 Hint : Perimeter = 2 (Length + Breadth) Area = Length x Breadth
#include <stdio.h>
int main() {
float length, breadth, perimeter, area;
// Input length and breadth
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &breadth);
// Calculate perimeter and area
perimeter = 2 * (length + breadth);
area = length * breadth;
// Output the results
printf("\nPerimeter of the rectangle = %.2f units\n", perimeter);
printf("Area of the rectangle = %.2f square units\n", area);
return 0;
}
OUTPUT
Enter the length of the rectangle: 5
Enter the breadth of the rectangle: 3
Perimeter of the rectangle = 16.00 units
Area of the rectangle = 15.00 square units
6. Write a C program to count the number of repetitive characters in a simple string. 20
Example : JANUARY o/p : Character A - Appeared 2 times.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int count[26] = {0}; // For A-Z
int i;
printf("Enter a string: ");
scanf("%s", str);
// Convert to uppercase and count character occurrences
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
char ch = toupper(str[i]);
count[ch - 'A']++;
}
}
// Display only characters that are repeated
printf("\nRepetitive characters:\n");
int found = 0;
for (i = 0; i < 26; i++) {
if (count[i] > 1) {
printf("Character %c - Appeared %d times.\n", i + 'A', count[i]);
found = 1;
}
}
if (!found) {
printf("No repetitive characters found.\n");
}
return 0;
}
OUTPUT
Enter a string: JANUARY
Repetitive characters:
Character A - Appeared 2 times.
7. Write a C program to find the ASCII value of any character given as input.
#include <stdio.h>
int main() {
char ch;
// Input character from user
printf("Enter a character: ");
scanf("%c", &ch);
// Display ASCII value
printf("The ASCII value of '%c' is: %d\n", ch, ch);
return 0;
}
OUTPUT
Enter a character: A
The ASCII value of 'A' is: 65
8. Write a C program to print the factors of a given number.
#include <stdio.h>
int main() {
int num, i;
// Input the number
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
// Loop to find and print factors
for (i = 1; i <= num; i++) {
if (num % i == 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
OUTPUT
Enter a positive integer: 12
Factors of 12 are: 1 2 3 4 6 12
9. Write a ‘C’ program to insert an element in a sorted array of ascending order.
#include <stdio.h>
int main() {
int arr[100], n, i, pos, element;
// Input size of array
printf("Enter number of elements in the array: ");
scanf("%d", &n);
// Input sorted array elements
printf("Enter %d elements in ascending order:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the element to be inserted
printf("Enter the element to insert: ");
scanf("%d", &element);
// Find the correct position for insertion
for (pos = 0; pos < n; pos++) {
if (element < arr[pos]) {
break;
}
}
// Shift elements to the right
for (i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
// Insert the element
arr[pos] = element;
n++; // Increase array size
// Print updated array
printf("Array after insertion:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT
Enter number of elements in the array: 5
Enter 5 elements in ascending order:
2 4 6 8 10
Enter the element to insert: 5
Array after insertion:
2 4 5 6 8 10
10. Write a ‘C’ program to add two matrices A (3×3) and B (3×3) and store the sum in matrix
C (3×3).
#include <stdio.h>
int main() {
int A[3][3], B[3][3], C[3][3];
int i, j;
// Input elements of Matrix A
printf("Enter elements of matrix A (3x3):\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
// Input elements of Matrix B
printf("\nEnter elements of matrix B (3x3):\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("B[%d][%d]: ", i, j);
scanf("%d", &B[i][j]);
}
}
// Add matrices A and B, store in C
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// Display the sum matrix C
printf("\nSum of matrices A and B (Matrix C):\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
Enter elements of matrix A (3x3):
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
A[2][0]: 7
A[2][1]: 8
A[2][2]: 9
Enter elements of matrix B (3x3):
B[0][0]: 9
B[0][1]: 8
B[0][2]: 7
B[1][0]: 6
B[1][1]: 5
B[1][2]: 4
B[2][0]: 3
B[2][1]: 2
B[2][2]: 1
Sum of matrices A and B (Matrix C):
10 10 10
10 10 10
10 10 10
11. Write a ‘C’ program to reverse a given a 5-digit number.
#include <stdio.h>
int main() {
int num, reverse = 0, digit, original;
// Input a 5-digit number
printf("Enter a 5-digit number: ");
scanf("%d", &num);
original = num;
// Check if it's a 5-digit number
if (num < 10000 || num > 99999) {
printf("Invalid input! Please enter a 5-digit number.\n");
return 1; // Exit the program
}
// Reverse the number
while (num != 0) {
digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
// Output
printf("The reverse of %d is %d\n", original, reverse);
return 0;
}
OUTPUT
Enter a 5-digit number: 12345
The reverse of 12345 is 54321
12. Write a ‘C’ program to find the product of 2 matrices A (2×2), B (2×2) and store the
product in matrix C (2×2).
#include <stdio.h>
int main() {
int A[2][2], B[2][2], C[2][2];
int i, j, k;
// Input elements of Matrix A
printf("Enter elements of Matrix A (2x2):\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
// Input elements of Matrix B
printf("\nEnter elements of Matrix B (2x2):\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("B[%d][%d]: ", i, j);
scanf("%d", &B[i][j]);
}
}
// Multiply matrices A and B, store result in C
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
C[i][j] = 0;
for (k = 0; k < 2; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Display the product matrix C
printf("\nProduct of matrices A and B (Matrix C):\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
Enter elements of Matrix A (2x2):
A[0][0]: 1
A[0][1]: 2
A[1][0]: 3
A[1][1]: 4
Enter elements of Matrix B (2x2):
B[0][0]: 5
B[0][1]: 6
B[1][0]: 7
B[1][1]: 8
Product of matrices A and B (Matrix C):
19 22
43 50
13. Write a c program to Implement a menu-driven application to manage books in a library
(add, display, search, etc.) using file handling.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book {
int id;
char title[100];
char author[100];
};
// Function to add a book
void addBook() {
struct Book b;
FILE *fp = fopen("[Link]", "a"); // Open in append mode
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
printf("Enter Book ID: ");
scanf("%d", &[Link]);
getchar(); // Clear newline from buffer
printf("Enter Book Title: ");
fgets([Link], sizeof([Link]), stdin);
[Link][strcspn([Link], "\n")] = '\0'; // Remove newline
printf("Enter Author Name: ");
fgets([Link], sizeof([Link]), stdin);
[Link][strcspn([Link], "\n")] = '\0';
fprintf(fp, "%d,%s,%s\n", [Link], [Link], [Link]);
fclose(fp);
printf("Book added successfully!\n");
}
// Function to display all books
void displayBooks() {
struct Book b;
FILE *fp = fopen("[Link]", "r");
if (fp == NULL) {
printf("No books found.\n");
return;
}
printf("\n%-10s %-30s %-30s\n", "Book ID", "Title", "Author");
printf("---------------------------------------------------------------\n");
while (fscanf(fp, "%d,%99[^,],%99[^\n]\n", &[Link], [Link], [Link]) == 3) {
printf("%-10d %-30s %-30s\n", [Link], [Link], [Link]);
}
fclose(fp);
}
// Function to search for a book by ID
void searchBook() {
int id, found = 0;
struct Book b;
FILE *fp = fopen("[Link]", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
printf("Enter Book ID to search: ");
scanf("%d", &id);
while (fscanf(fp, "%d,%99[^,],%99[^\n]\n", &[Link], [Link], [Link]) == 3) {
if ([Link] == id) {
printf("\nBook Found:\n");
printf("ID: %d\nTitle: %s\nAuthor: %s\n", [Link], [Link], [Link]);
found = 1;
break;
}
}
if (!found) {
printf("Book with ID %d not found.\n", id);
}
fclose(fp);
}
// Main function: menu-driven loop
int main() {
int choice;
do {
printf("\n--- Library Management Menu ---\n");
printf("1. Add Book\n");
printf("2. Display All Books\n");
printf("3. Search Book by ID\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // clear buffer
switch (choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
searchBook();
break;
case 4:
printf("Exiting the program. Goodbye!\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 4);
return 0;
}
OUTPUT
--- Library Management Menu ---
1. Add Book
2. Display All Books
3. Search Book by ID
4. Exit
Enter your choice: 1
Enter Book ID: 101
Enter Book Title: Programming in C
Enter Author Name: E. Balagurusamy
Book added successfully!
14. Write a c program to implement string functions (like finding length, concatenation,
comparison, or copy) without using built-in functions.
#include <stdio.h>
// Function to find length of a string
int stringLength(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
// Function to concatenate two strings
void stringConcat(char str1[], char str2[]) {
int i = 0, j = 0;
// Move i to end of str1
while (str1[i] != '\0') {
i++;
}
// Copy str2 to the end of str1
while (str2[j] != '\0') {
str1[i++] = str2[j++];
}
str1[i] = '\0'; // Null terminate
}
// Function to compare two strings
int stringCompare(char str1[], char str2[]) {
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return str1[i] - str2[i]; // Non-zero if not equal
}
i++;
}
return str1[i] - str2[i]; // Handles if lengths are different
}
// Function to copy one string to another
void stringCopy(char dest[], char src[]) {
int i = 0;
while (src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
int main() {
char str1[100], str2[100], copied[100];
int choice;
do {
printf("\n--- String Function Menu ---\n");
printf("1. Find Length of String\n");
printf("2. Concatenate Strings\n");
printf("3. Compare Strings\n");
printf("4. Copy String\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear newline
switch (choice) {
case 1:
printf("Enter a string: ");
fgets(str1, sizeof(str1), stdin);
str1[stringLength(str1) - 1] = '\0'; // remove newline
printf("Length = %d\n", stringLength(str1));
break;
case 2:
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
str1[stringLength(str1) - 1] = '\0';
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
str2[stringLength(str2) - 1] = '\0';
stringConcat(str1, str2);
printf("Concatenated String: %s\n", str1);
break;
case 3:
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
str1[stringLength(str1) - 1] = '\0';
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
str2[stringLength(str2) - 1] = '\0';
int result = stringCompare(str1, str2);
if (result == 0)
printf("Strings are equal.\n");
else if (result < 0)
printf("First string is smaller.\n");
else
printf("First string is greater.\n");
break;
case 4:
printf("Enter source string: ");
fgets(str2, sizeof(str2), stdin);
str2[stringLength(str2) - 1] = '\0';
stringCopy(copied, str2);
printf("Copied String: %s\n", copied);
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
return 0;
}
OUTPUT
--- String Function Menu ---
1. Find Length of String
2. Concatenate Strings
3. Compare Strings
4. Copy String
5. Exit
Enter your choice: 1
Enter a string: Hello
Length = 5