BCA Sem1 C - Programming
BCA Sem1 C - Programming
### 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:
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack.
```c
#include <stdio.h>
if (n == 1) {
return;
int main() {
return 0;
```
- `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() {
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;.
if (F_Ptr == NULL) {
} else {
fclose(F_Ptr);
return 0;
```
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
int main() {
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.