BCA lab questions with solution
BCA lab questions with solution
Write a c program to find the sum, average and standard deviation for a given
set of numbers.
#include <stdio.h>
#include <math.h>
int main() {
int n;
double sum = 0.0, average, variance = 0.0, stddev;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Number of elements must be greater than 0.\n");
return 1;
}
double numbers[n];
printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%lf", &numbers[i]);
sum += numbers[i];
}
average = sum / n;
for (int i = 0; i < n; i++) {
variance += pow(numbers[i] - average, 2);
}
stddev = sqrt(variance / n);
printf("Sum = %.2lf\n", sum);
printf("Average = %.2lf\n", average);
printf("Standard Deviation = %.2lf\n", stddev);
return 0;
}
printf("\n");
return 0;
}
3. Write a c program to generate Fibonacci series.
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
}
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
printf("\n");
return 0;
}
4. Write a c program to print magic square of order n where n>3 and n is odd.
#include<stdio.h>
int main()
{
printf("Enter the number of rows and columns: ");
int n;
scanf("%d",&n);
int i,j,a=1;
int ms[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
ms[i][j]=0;
}
}
if(n%2==0 || n<=3)
{
printf("Invalid input");
}
else
{
i=0,j=n/2;
while(a<=n*n)
{
ms[i][j]=a;
a++;
int nxt_r=(i-1+n)%n;
int nxt_c=(j+1)%n;
if(ms[nxt_r][nxt_c]!=0)
{
i=(i+1)%n;
}
else
{
i=nxt_r;
j=nxt_c;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",ms[i][j]);
}
printf("\n");
}
}
}}
Input/Output:
Enter the order of the magic square (must be odd and greater than 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
Input/Output:
Enter a sentence: This is a sample sentence with vowels.
Number of vowels in the sentence: 11
9. Write a c program to print the students Mark sheet assuming roll number,
name, and marks in 5 subjects in a structure. Create an array of structures
and print the mark sheet in the university pattern.
#include <stdio.h>
// Define a structure to represent a student's mark sheet
struct Student {
int rollNumber;
char name[50];
float marks[5];
};
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Number of students must be greater than 0.\n");
return 1;
}
struct Student students[n];
// Input marks for each student
for (int i = 0; i < n; i++) {
printf("Enter details for Student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
printf("Name: ");
scanf("%s", students[i].name);
printf("Enter marks for 5 subjects:\n");
for (int j = 0; j < 5; j++) {
printf("Subject %d: ", j + 1);
scanf("%f", &students[i].marks[j]);
}
}
// Print the mark sheets in university pattern
printf("\nUniversity Mark Sheet\n\n");
for (int i = 0; i < n; i++) {
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Name: %s\n", students[i].name);
printf("Marks:\n");
for (int j = 0; j < 5; j++) {
printf("Subject %d: %.2f\n", j + 1, students[i].marks[j]);
}
printf("\n");
}
return 0;
}
Input/Output:
Enter the number of students: 2
Enter details for Student 1:
Roll Number: 101
Name: John
Enter marks for 5 subjects:
Subject 1: 95
Subject 2: 87
Subject 3: 76
Subject 4: 88
Subject 5: 92
10. Write a function using pointers to add two matrices and to return the
resultant matrix of the calling function.
#include<stdio.h>
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.
#include <stdio.h>
#include <string.h>
int main() {
char file1_name[100], file2_name[100];
printf("Enter the name of the first file: ");
scanf("%s", file1_name);
printf("Enter the name of the second file: ");
scanf("%s", file2_name);
FILE *file1 = fopen(file1_name, "r");
FILE *file2 = fopen(file2_name, "r");
if (file1 == NULL || file2 == NULL) {
printf("Error opening files.\n");
return 1;
}
int same_contents = 1; // Assume contents are the same
int ch1, ch2;
while ((ch1 = fgetc(file1)) != EOF && (ch2 = fgetc(file2)) != EOF) {
if (ch1 != ch2) {
same_contents = 0; // Contents are not the same
break;
}
}
fclose(file1);
fclose(file2);
if (same_contents) {
if (remove(file2_name) == 0) {
printf("File contents are the same. Second file '%s' deleted.\n", file2_name);
} else {
printf("Error deleting file '%s'.\n", file2_name);
}
} else {
printf("File contents are not the same.\n");
}
return 0;
}
Input/Output:
Enter the name of the first file: file1.txt
Enter the name of the second file: file2.txt
File contents are the same. Second file 'file2.txt' deleted.
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) Number
of chairs (II) Number of words and (III) Number of lines.
#include <stdio.h>
int main() {
char source_filename[100], destination_filename[100];
char ch;
int chairs = 0, words = 0, lines = 0;
printf("Enter the name of the source file: ");
scanf("%s", source_filename);
printf("Enter the name of the destination file: ");
scanf("%s", destination_filename);
FILE *source = fopen(source_filename, "r");
FILE *destination = fopen(destination_filename, "w");
if (source == NULL || destination == NULL) {
printf("Error opening files.\n");
return 1;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
if (ch == ' ' || ch == '\t' || ch == '\n') {
words++;
if (ch == '\n') {
lines++;
}
} else if (ch == 'c' || ch == 'C') {
char next_ch = fgetc(source);
if (next_ch == 'h' || next_ch == 'H') {
chairs++;
}
ungetc(next_ch, source); // Put back the character for future reading
}
}
fprintf(destination, "\n(I) Number of chairs: %d\n", chairs);
fprintf(destination, "(II) Number of words: %d\n", words);
fprintf(destination, "(III) Number of lines: %d\n", lines);
fclose(source);
fclose(destination);
printf("File copied and statistics appended to '%s'.\n", destination_filename);
return 0;
}
Input/Output:
Enter the name of the source file: source.txt
Enter the name of the destination file: destination.txt
File copied and statistics appended to 'destination.txt'.