0% found this document useful (0 votes)
6 views

BCA lab questions with solution

The document contains a series of C programming exercises that cover various topics such as calculating sum, average, and standard deviation, generating prime numbers, Fibonacci series, magic squares, sorting numbers, checking for palindromes, counting vowels, calculating factorials, printing student mark sheets, matrix addition, file comparison, and file copying with statistics. Each exercise includes code snippets with input/output examples. The programs demonstrate fundamental programming concepts and techniques in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

BCA lab questions with solution

The document contains a series of C programming exercises that cover various topics such as calculating sum, average, and standard deviation, generating prime numbers, Fibonacci series, magic squares, sorting numbers, checking for palindromes, counting vowels, calculating factorials, printing student mark sheets, matrix addition, file comparison, and file copying with statistics. Each exercise includes code snippets with input/output examples. The programs demonstrate fundamental programming concepts and techniques in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

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;
}

2. Write a c program to generate n prime numbers.


#include <stdio.h>
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. n should be a positive integer.\n");
return 1;
}
printf("First %d prime numbers are:\n", n);
int count = 0;
int num = 2;
while (count < n) {
int is_prime = 1; // Assume num is prime
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
is_prime = 0; // Not prime
break;
}
}
if (is_prime) {
printf("%d ", num);
count++;
}
num++;
}

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

5. Write a c program to sort the given set of numbers in ascending order.


#include <stdio.h>
int main() {
int n, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int numbers[n];
printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
// Bubble Sort Algorithm
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
// Swap numbers[j] and numbers[j+1]
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
printf("Sorted numbers in ascending order:\n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Input/Output:
Enter the number of elements: 5
Enter 5 numbers:
83152
Sorted numbers in ascending order:
12358

6. Write a c program to check whether the given string is a palindrome or not


using pointers.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
int len = strlen(str);
bool isPalindrome = true;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
printf("%s is a palindrome.\n", str);
} else {
printf("%s is not a palindrome.\n", str);
}
return 0;
}
Input/Output:
Enter a string: racecar
racecar is a palindrome.

7. Write a c program to count the number of Vowels in the given sentence.


#include <stdio.h>
#include <ctype.h>
int main() {
char sentence[1000];
int count = 0;
printf("Enter a sentence: ");
gets(sentence);
for (int i = 0; sentence[i] != '\0'; i++) {
char ch = tolower(sentence[i]); // Convert character to lowercase for case insensitivity
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
printf("Number of vowels in the sentence: %d\n", count);
return 0;
}

Input/Output:
Enter a sentence: This is a sample sentence with vowels.
Number of vowels in the sentence: 11

8. Write a c program to find the factorial of a given number using recursive


function.
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
int result = factorial(num);
printf("Factorial of %d is %d\n", num, result);
}
return 0;
}
Input/Output:
Enter a number: 5
Factorial of 5 is 120

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

Enter details for Student 2:


Roll Number: 102
Name: Alice
Enter marks for 5 subjects:
Subject 1: 78
Subject 2: 85
Subject 3: 91
Subject 4: 79
Subject 5: 87

University Mark Sheet


Roll Number: 101
Name: John
Marks:
Subject 1: 95.00
Subject 2: 87.00
Subject 3: 76.00
Subject 4: 88.00
Subject 5: 92.00
Roll Number: 102
Name: Alice
Marks:
Subject 1: 78.00
Subject 2: 85.00
Subject 3: 91.00
Subject 4: 79.00
Subject 5: 87.00

10. Write a function using pointers to add two matrices and to return the
resultant matrix of the calling function.
#include<stdio.h>

void add(int n,int m,int *a,int *b,int *ans)


{
for(int i=0;i<n*m;i++)
{
ans[i] = a[i]+b[i];
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int a[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
int b[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&b[i][j]);
}
}
int ans[n][m];
add(n,m,&a[0][0],&b[0][0],&ans[0][0]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
printf("%d ",ans[i][j]);
}
printf("\n");
}
return 0;
}
Input/Output:
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements for the first matrix:
12
34
Enter elements for the second matrix:
56
78
Resultant matrix after addition:
68
10 12

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.

Enter the name of the first file: file1.txt


Enter the name of the second file: file3.txt
File contents are not the same.

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'.

You might also like