#include <stdio.h> void circle(int radius) { /* Center of the circle */ int centRow = radius, centCol = radius; int r, c; const int adjust=3; for ( r=0; r<=radius*2; r+=1 ) { for ( c=0; c<=radius*2; c+=1 ) if ( (r-centRow)*(r-centRow) + (c-centCol)*(c-centCol) < radius*radius-adjust ) putchar('*'); else putchar(' '); putchar('\n'); } } int main(int argc, char *argv[]) { circle(9); system("PAUSE"); return 0; }
Comments: There is no need to compute square roots. Comparing the squared distances gives the same results.
The value adjust
is used to make the printed circle look more
like a circle than the formula gives.
The calculation could be optimized by moving part of it outside of the inner loop.