#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define FAN_SIZE 5
// Function to clear the console screen
void clearScreen() {
system("clear"); // For Linux/Mac
// system("cls"); // For Windows
}
// Function to draw the fan
void drawFan(int angle) {
int i, j;
// Clear the screen
clearScreen();
// Draw the fan blades
for (i = 0; i < FAN_SIZE; i++) {
for (j = 0; j < FAN_SIZE; j++) {
if (i == FAN_SIZE / 2 && j == FAN_SIZE / 2) {
printf(" * ");
} else if (i == FAN_SIZE / 2) {
printf(" - ");
} else if (j == FAN_SIZE / 2) {
printf(" | ");
} else {
printf(" ");
}
}
printf("\n");
}
// Draw the angle of rotation
printf("\nAngle of Rotation: %d degrees\n", angle);
// Add a delay for animation effect
usleep(100000); // Sleep for 100 milliseconds (100000 microseconds)
}
int main() {
int angle = 0;
// Rotate the fan continuously
while (1) {
drawFan(angle);
angle = (angle + 15) % 360; // Increment angle by 15 degrees for each rotation
}
return 0;
}
COutput:
Explanation:
1. Header Files:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
CThe necessary standard C library header files are included: stdio.h for input/output operations, stdlib.h for system-related functions, and unistd.h for the usleep() function, which enables us to add a delay for the animation effect.
2. Macro Definition:
#define FAN_SIZE 5
CThe FAN_SIZE macro defines the size of the fan, which determines the number of rows and columns for the fan drawing.
3. clearScreen() Function:
void clearScreen() {
system("clear"); // For Linux/Mac
// system("cls"); // For Windows
}
CThe clearScreen() function is used to clear the console screen. Depending on the platform (Linux/Mac or Windows), it uses either “clear” or “cls” command to clear the screen.
4. drawFan() Function:
void drawFan(int angle) {
// ...
}
CThe drawFan() function is responsible for drawing the fan on the console screen. It takes an angle parameter, which represents the current angle of rotation of the fan blades.
5. Drawing the Fan:
// Draw the fan blades
for (i = 0; i < FAN_SIZE; i++) {
for (j = 0; j < FAN_SIZE; j++) {
// ...
}
printf("\n");
}
CThe nested loops are used to draw the fan blades. It iterates through each row and column of the FAN_SIZE, and based on the positions, it prints appropriate ASCII characters to create the fan blades.
6. Updating the Rotation Angle:
int main() {
int angle = 0;
// Rotate the fan continuously
while (1) {
drawFan(angle);
angle = (angle + 15) % 360; // Increment angle by 15 degrees for each rotation
}
return 0;
}
CIn the main() function, an infinite loop is used to rotate the fan continuously. It calls the drawFan() function with the current angle and then increments the angle by 15 degrees for the next rotation. The % 360 operation is used
7. Animation Effect:
// Add a delay for animation effect
usleep(100000); // Sleep for 100 milliseconds (100000 microseconds)
CThe usleep() function is used to introduce a small delay (100 milliseconds) between each fan rotation. This creates the animation effect, making it look like the fan is rotating smoothly.