# C program to draw a circle

# C program to draw a circle

Drawing a circle involves plotting a set of points that lie on the circumference of the circle. One common approach is to use the Cartesian coordinate system and iterate over the x and y coordinates to determine whether each point is within the circle's radius.

By printing specific characters or symbols, we can create the illusion of a circle on the screen.

**C Program:**

#include <stdio.h>

int main() {

int radius, centerX, centerY, x, y;

printf("Enter the radius of the circle: ");

scanf("%d", &radius);

printf("Enter the x-coordinate of the center: ");

scanf("%d", ¢erX);

printf("Enter the y-coordinate of the center: ");

scanf("%d", ¢erY);

for (y = centerY + radius; y >= centerY - radius; y--) {

for (x = centerX - radius; x <= centerX + radius; x++) {

int dx = x - centerX;

int dy = y - centerY;

int distanceSquared = dx * dx + dy * dy;

if (distanceSquared <= radius * radius) {

printf("*");

} else {

printf(" ");

}

}

printf("\n");

}

return 0;

}

**Explanation of the C program:**

- The program begins by including the necessary header file stdio.h, which provides the input/output functions in C.
- The main() function is the entry point of the program.

Inside the main() function, we declare the variables radius, centerX, centerY, x, and y.

The radius variable stores the radius of the circle, while centerX and centerY store the coordinates of the circle's center. x and y will be used for iterating over the Cartesian coordinates.

- The program prompts the user to enter the radius, x-coordinate of the center, and y-coordinate of the center of the circle using printf() and scanf() functions.
- The program uses nested for loops to iterate over the y-coordinate from the top to the bottom of the circle and the x-coordinate from the leftmost to the rightmost points of the circle.
- Inside the nested loops, the program calculates the distance between the current point (x, y) and the center of the circle (centerX, centerY) using the distance formula: distanceSquared = (x - centerX)^2 + (y - centerY)^2.
- The program then compares the calculated distanceSquared with the square of the radius to determine if the current point is within the circle. If it is, the program prints an asterisk * using printf("*"), indicating a point on the circle's circumference. Otherwise, it prints a space character " " using printf(" ").
- After printing all the characters for a row, the program moves to the next line using printf("\n").
- The loops continue until all the points within the circle's boundary are iterated and plotted.
- Finally, the program returns 0 to indicate successful execution.
- This program allows the user to enter the radius and the coordinates of the center of the circle, and it then prints a representation of the circle on the screen using asterisks * to approximate the shape. Keep in mind that the appearance of the circle might not be perfect due to the limitations of ASCII art and the aspect ratio of characters in the console or terminal. For more accurate and precise circle drawing, specialized graphics libraries or APIs

**Output:**

**Explanation:**

In the given example, the circle appears as expected with a radius of 5, centred at coordinates (10, 5).

The program prompts the user to enter the radius of the circle, x-coordinate of the center, and y-coordinate of the center.

In the given example, the user enters a radius of 5, x-coordinate of 10, and y-coordinate of 5.

The program then proceeds to iterate over the y-coordinates from 10+5 (topmost point of the circle) to 10-5 (bottommost point of the circle).

For each y-coordinate, the program iterates over the x-coordinates from 10-5 (leftmost point of the circle) to 10+5 (rightmost point of the circle).

Inside the nested loops, the program calculates the distance between the current point (x, y) and the centre of the circle (10, 5) using the distance formula.

The program compares the calculated distance squared with the square of the radius to determine whether the current point lies within the circle's circumference.

If the distance squared is less than or equal to the radius squared, the program prints an asterisk (*) to represent a point on the circle's circumference.

If the distance squared is greater than the radius squared, the program prints a space character to represent a point outside the circle.

After printing all the characters for a row, the program moves to the next line.

The loops continue until all the points within the circle's boundary are iterated and plotted.

The resulting output is a representation of the circle on the screen, with asterisks (*) approximating the circle's shape.