CS3251 - Prgramming in C SET II
CS3251 - Prgramming in C SET II
No:
2. Simplifies complex problems like tree traversal, factorial, Fibonacci, etc. 2 BL1 CO3
3. 2 BL2 CO3
4. 2 BL2 CO4
8. 2 BL1 CO5
PART – B (5 16 = 80 Marks)
Explain about pointers and write the use of pointers in arrays with
suitable example.
Pointers are variables that store memory addresses. A pointer to an array
points to the first element of the array.
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
11. A int *ptr = arr; 16 BL3 CO3
return 0;
}
OR
11. B What is recursion? Write a C program to find the sum of the 16 BL3 CO3
digits, to find the factorial of a number and binary search using
recursion.
Recursion is a technique where a function calls itself.
int sumDigits(int n) {
if (n == 0) return 0;
return (n % 10) + sumDigits(n / 10);
}
Factorial using Recursion:
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
Binary Search using Recursion:
int binarySearch(int arr[], int low, int high, int key) {
if (low > high) return -1;
int mid = (low + high) / 2;
if (arr[mid] == key) return mid;
else if (key < arr[mid]) return binarySearch(arr, low, mid - 1,
key);
else return binarySearch(arr, mid + 1, high, key);
}
Explain in detail about function and its types in C with example
program.
OR
Explain the concept of pass by value and pass by reference. Write a
C program to swap the content of two variables using pass by
reference.
Pass by Value: Copies the value (default in C)
Pass by Reference: Uses pointers to directly modify original variables
#include <stdio.h>
void swap(int *x, int *y) {
12. B int temp = *x; 16 BL3 CO3
*x = *y;
*y = temp;
}
int main() {
int a = 10, b = 20;
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
13. A Explain in detail about various storage class with suitable example 16 BL2 CO4
auto: Default for local variables
#include <stdio.h>
void testStatic() {
static int x = 0;
x++;
int main() {
testStatic();
testStatic();
return 0;
OR
13. B(i) Explain about singly linked list with suitable example C program. 8 BL2 CO4
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
int *arr, n;
printf("Enter size: ");
scanf("%d", &n);
B(ii) 8 BL2 CO4
arr = (int*) malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
struct Employee {
int id;
char name[30];
};
int main() {
struct Employee emp[3] = {{101, "John"}, {102, "Alice"}, {103,
"Bob"}};
int searchId;
printf("Enter ID to search: ");
scanf("%d", &searchId);
15 A Explain in detail about structure (Nested structure, Pointer and 16 BL2 CO5
structure, array of structure) in c with example program,
Nested Structure, Pointer to Structure, Array of Structure:
#include <stdio.h>
struct Date {
int day, month, year;
};
struct Student {
int id;
char name[30];
struct Date dob; // Nested structure
};
int main() {
struct Student students[2] = {
{1, "John", {1, 1, 2000}},
{2, "Alice", {2, 2, 2001}}
};
struct Student *ptr = &students[0]; // Pointer to structure
printf("Student: %s, DOB: %d/%d/%d\n", ptr->name, ptr-
>dob.day, ptr->dob.month, ptr->dob.year);
return 0;
}
OR
Write a C program to store the employee information using
structure and search a particular employee using employee number?
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[30];
};
int main() {
struct Employee emp[3] = {{101, "John"}, {102, "Alice"}, {103,
"Bob"}};
15. B(i) 8 BL1 CO5
int searchId;
printf("Enter ID to search: ");
scanf("%d", &searchId);
struct Student {
int id;
char name[30];
float marks;
};
int main() {
struct Student s = {1, "John", 85.5};
char grade;