0% found this document useful (0 votes)
175 views9 pages

C Programming Solved Questions 2022-2024

The document contains solved questions from BCA-202 C Programming for May 2024 and June 2022, covering topics such as header files, macros vs functions, sorting integers, checking palindromes, and matrix multiplication. It includes C code examples for various programming tasks and key questions about arrays, pointers, and structures. Additionally, it addresses concepts like automatic vs static variables and function pointers.
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)
175 views9 pages

C Programming Solved Questions 2022-2024

The document contains solved questions from BCA-202 C Programming for May 2024 and June 2022, covering topics such as header files, macros vs functions, sorting integers, checking palindromes, and matrix multiplication. It includes C code examples for various programming tasks and key questions about arrays, pointers, and structures. Additionally, it addresses concepts like automatic vs static variables and function pointers.
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

BCA-202 C Programming

Solved Questions - May 2024 & June 2022

MAY 2024 - Section A

1. What are header files and their uses?


Header files contain declarations of functions, macros, and variables to be shared across
multiple source files. Examples include #include <stdio.h> for input/output, #include
<stdlib.h> for general utilities, etc.

2. What is the difference between macro and function?


A macro is a preprocessor directive that defines constants or code fragments. It is
expanded before compilation. A function is a block of code executed at runtime. Macros
are faster but less safe; functions are safer and type-checked.

MAY 2024 - Section B

6. C program to read 20 integers and sort in ascending order:

#include <stdio.h>

int main() {

int arr[20], i, j, temp;

printf("Enter 20 integers: ");

for(i = 0; i < 20; i++)

scanf("%d", &arr[i]);

for(i = 0; i < 20; i++) {

for(j = i + 1; j < 20; j++) {

if(arr[i] > arr[j]) {

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}
}

printf("Sorted array: ");

for(i = 0; i < 20; i++)

printf("%d ", arr[i]);

return 0;

7. C program to check palindrome without string library:

#include <stdio.h>

int main() {

char str[100];

int i, len = 0, flag = 1;

printf("Enter a string: ");

scanf("%s", str);

while(str[len] != '\0') len++;

for(i = 0; i < len/2; i++) {

if(str[i] != str[len-i-1]) {

flag = 0;

break;

if(flag) printf("Palindrome\n");

else printf("Not Palindrome\n");

return 0;

8. Add two complex numbers using function:


#include <stdio.h>

typedef struct {

float real, imag;

} Complex;

Complex add(Complex a, Complex b) {

Complex result;

[Link] = [Link] + [Link];

[Link] = [Link] + [Link];

return result;

int main() {

Complex x = {2.5, 3.5}, y = {1.5, 2.5}, z;

z = add(x, y);

printf("Sum = %.1f + %.1fi\n", [Link], [Link]);

return 0;

MAY 2024 - Section C

11. Program to count number of words and characters in a file:

#include <stdio.h>

#include <ctype.h>

int main() {

FILE *fp;

char ch;

int words = 0, chars = 0, inWord = 0;

fp = fopen("[Link]", "r");
if(fp == NULL) {

printf("File not found!\n");

return 1;

while((ch = fgetc(fp)) != EOF) {

chars++;

if(isspace(ch)) inWord = 0;

else if(!inWord) {

words++;

inWord = 1;

fclose(fp);

printf("Words: %d, Characters: %d\n", words, chars);

return 0;

12. Program for multiplication of 4x4 matrices:

#include <stdio.h>

int main() {

int a[4][4], b[4][4], c[4][4] = {0};

printf("Enter first 4x4 matrix:\n");

for(int i = 0; i < 4; i++)

for(int j = 0; j < 4; j++)

scanf("%d", &a[i][j]);

printf("Enter second 4x4 matrix:\n");


for(int i = 0; i < 4; i++)

for(int j = 0; j < 4; j++)

scanf("%d", &b[i][j]);

for(int i = 0; i < 4; i++)

for(int j = 0; j < 4; j++)

for(int k = 0; k < 4; k++)

c[i][j] += a[i][k] * b[k][j];

printf("Result matrix:\n");

for(int i = 0; i < 4; i++) {

for(int j = 0; j < 4; j++)

printf("%d ", c[i][j]);

printf("\n");

return 0;

13. Structure for books and most expensive book:

#include <stdio.h>

#include <string.h>

typedef struct {

char title[50];

char author[50];

float price;

} Book;

int main() {
Book b[3];

int i, maxIndex = 0;

for(i = 0; i < 3; i++) {

printf("Enter title, author, and price of book %d:\n", i+1);

scanf("%s %s %f", b[i].title, b[i].author, &b[i].price);

for(i = 1; i < 3; i++)

if(b[i].price > b[maxIndex].price)

maxIndex = i;

printf("Most expensive book: %s by %s - Rs %.2f\n", b[maxIndex].title, b[maxIndex].author,


b[maxIndex].price);

return 0;

JUNE 2022 - Key Questions & Answers

1. Need for array variables: Arrays help store multiple values of the same type using a
single variable name, ideal for processing lists, matrices, etc.

2. Difference between automatic and static variables: Automatic variables are local and
created at function entry, destroyed on exit. Static variables retain their value across
function calls.

3. Array of structures: A way to store multiple records, e.g., students with roll, name,
marks. Enables batch processing.

4. Pointers - definition and uses: A pointer holds memory address of a variable. Used in
dynamic memory, arrays, functions, etc.

5. Function getc(): Reads a single character from a file. Advantage: allows low-level
character I/O, limitation: buffered and slower than direct memory I/O.

6. Structure and difference from union: Structure can hold different data types, all at once.
Union can store one member at a time using shared memory.
7. Logical bitwise operators:

• & (AND), | (OR), ^ (XOR). Useful in masks, toggling bits, flags, compression.

8. Difference between array name and pointer: Array name gives base address, pointer is
variable storing address. Pointer can be reassigned, array name cannot.

9. Function as argument to another function: Possible via function pointers.

10. Program to print ASCII table using string and format:

#include <stdio.h>

int main() {

for(int i = 0; i < 128; i++)

printf("%d: %c\n", i, i);

return 0;

11. Explain (a) Struct keyword (b) Typedef keyword

• struct declares structures; typedef defines aliases for data types.

12. Function to insert value in ordered array:

#include <stdio.h>

int main() {

int arr[100], n, x, i, pos;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter sorted array: ");

for(i = 0; i < n; i++) scanf("%d", &arr[i]);

printf("Enter value to insert: ");

scanf("%d", &x);

for(pos = 0; pos < n && arr[pos] < x; pos++);

for(i = n; i > pos; i--) arr[i] = arr[i-1];


arr[pos] = x; n++;

printf("Updated array: ");

for(i = 0; i < n; i++) printf("%d ", arr[i]);

return 0;

13. Bitwise operators & sorting program using array:

#include <stdio.h>

int main() {

int a[10], i, j, temp;

printf("Enter 10 numbers: ");

for(i = 0; i < 10; i++) scanf("%d", &a[i]);

for(i = 0; i < 10; i++) {

for(j = i+1; j < 10; j++) {

if(a[i] > a[j]) {

temp = a[i];

a[i] = a[j];

a[j] = temp;

printf("Sorted: ");

for(i = 0; i < 10; i++) printf("%d ", a[i]);

return 0;

END OF SOLUTIONS

You might also like