This program utilizes the equation of circle to draw a circle instead of using a graphics library. It makes use of the coordinates of x and y and graphs the circle according to that information. Let us assume that the circle has a center at (0,0). Think in term of limits of the for loops, i.e., where are the corner coordinates of the circle; and then applying an if condition to achieve our required goal.
#include <stdio.h> int main() { //Author : Faizan int adia,arow,acol,aradius; printf("\t\t * * * * * \n\t PROGRAM TO PRINT A CIRCLE \n\t\t * * * * * \n"); printf("\n Please Enter Diameter : "); scanf_s("%d",&adia); aradius=adia/2; // radius determination for (arow=aradius; arow>=-arradis; arow--) // y-coordinate { printf("\n\n"); for (acol=-aradius; acol<=aradius; acol++){ // x-coordinate if (acol*acol + arow*arow < aradius*aradius) //equation of the circle printf("# "); else printf(" "); } } printf("\n"); system("pause"); }
Running the above code yields a diameter x diameter grid and then restricts the printing of building character (here '#') to a circular form as shown in the screenshot at the top of this page.
No comments:
Post a Comment