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

BCA Sem1 C - Programming

Q&A
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

BCA Sem1 C - Programming

Q&A
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Here are the answers to the questions provided in your assignment.

### 1. Explain the Tower of Hanoi. Write a C program to illustrate the Tower of Hanoi using C.

The Tower of Hanoi is a mathematical puzzle consisting of three rods and a number of disks of different sizes which
can slide onto any rod. The puzzle starts with the disks neatly stacked in ascending order of size on one rod, the
smallest at the top. The objective of the puzzle is to move the entire stack to another rod, obeying the following
rules:

1. Only one disk can be moved at a time.

2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack.

3. No disk may be placed on top of a smaller disk.

#### C Program to Illustrate the Tower of Hanoi

```c

#include <stdio.h>

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {

if (n == 1) {

printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);

return;

towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);

printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);

towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);

int main() {

int n = 4; // Number of disks

towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

return 0;

```

### 2. Explain the difference between `calloc()` and `malloc()`.

- `malloc()`: Allocates a block of memory of specified size. The contents of the allocated memory are not initialized
and may contain garbage values.

```c
int *ptr = (int*)malloc(5 * sizeof(int)); // Allocates memory for 5 integers

```

- `calloc()`: Allocates memory for an array of elements, initializes all bytes to zero.

```c

int *ptr = (int*)calloc(5, sizeof(int)); // Allocates memory for 5 integers and initializes all to 0

```

### 3. What do you mean by the pointer in C? Explain pointers with an example.

A pointer in C is a variable that stores the address of another variable. Pointers are used for dynamic memory
allocation, array manipulation, and implementing data structures like linked lists and trees.

#### Example

```c

#include <stdio.h>

int main() {

int var = 10; // Declare an integer variable

int *ptr; // Declare a pointer to an integer

ptr = &var; // Store the address of var in ptr

printf("Value of var: %d\n", var);

printf("Address of var: %p\n", &var);

printf("Value of ptr (address of var): %p\n", ptr);

printf("Value pointed to by ptr: %d\n", *ptr);

return 0;

```

### 4. Write a C program to open a file, write in it, and close the file.

```c

#include <stdio.h>
int main() {

FILE *F_Ptr;.

F_Ptr = fopen("example.txt", "w");

if (F_Ptr == NULL) {

printf("File failed to open.\n");

} else {

printf("File opened successfully.\n");

fprintf(F_Ptr, "Hello, this is a test file.\n");

fclose(F_Ptr);

printf("File closed successfully.\n");

return 0;

```

### 5. What is the use of `#define` preprocessor? Explain with an example.

The `#define` preprocessor directive is used to define macros. Macros are constants or expressions that can be
replaced by a specific value or code fragment throughout the program. It allows for easier code maintenance and
readability.

#### Example

```c

#include <stdio.h>

#define PI 3.14159

#define AREA_OF_CIRCLE(radius) (PI * (radius) * (radius))

int main() {

float radius = 5.0;

printf("Area of the circle with radius %.2f is %.2f\n", radius, AREA_OF_CIRCLE(radius));

return 0;

}
```

In this example, `PI` is defined as a constant, and `AREA_OF_CIRCLE` is a macro that calculates the area of a circle
given its radius. The preprocessor replaces these macros with their respective values before the compilation of the
program.

You might also like