Monday, April 1, 2013

The circle program (from keyboard)

A program that ases the user for the diameter of a circle and prints out its circumference and area.

#include <stdio.h>
#define PI 3.1416

int
main (void)
{
     /* declarations */
     double diam, area, circ, r;
 
     /* get diameter from user */
     printf ("Enter a value for the diameter: ");
     scanf ("%lf", &diam);
        
     /* do the computations */
     r = diam / 2;
     area = PI * r * r;
     circ = 2 * PI * r;
 
     /* display the report on the screen */
     printf ("\nA circle with a diameter of %3.1lf cm, ", diam);
     printf ("has an area of %5.3lf cm2\n", area);
     printf ("and a circumference of %4.2lf cm.\n", circ);
 
     return (0);
}

Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
Enter a value for the diameter: 10

A circle with a diameter of 10.0 cm, has an area of 78.540 cm2
and a circumference of 31.42 cm.

No comments:

Post a Comment