Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Write a c program to rotate a fan in graphic ?

C
#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;
}
C

Output:

Explanation:

1. Header Files:

C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
C

The 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:

C
#define FAN_SIZE 5
C

The FAN_SIZE macro defines the size of the fan, which determines the number of rows and columns for the fan drawing.

3. clearScreen() Function:

C
void clearScreen() {
    system("clear"); // For Linux/Mac
    // system("cls"); // For Windows
}
C

The 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:

C
void drawFan(int angle) {
    // ...
}
C

The 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:

C
// Draw the fan blades
for (i = 0; i < FAN_SIZE; i++) {
    for (j = 0; j < FAN_SIZE; j++) {
        // ...
    }
    printf("\n");
}
C

The 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:

C
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;
}
C

In 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:

C
// Add a delay for animation effect
usleep(100000); // Sleep for 100 milliseconds (100000 microseconds)
C

The 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.