Cpp - Write program to calculate the circumference and area of a circle

Requirements

Write a void type function called circle() to calculate the circumference and area of a circle.

The radius and two variables are passed to the function, which therefore has three parameters:

  • A read-only reference to double for the radius and two
  • references to double that the function uses to store the area
  • and circumference of the circle.

Hint

Given a circle with radius r:

Area = pi * r * r and circumference = 2 * pi * r where p = 3.1415926536

Test the function circle() by outputting a table containing the radius, the circumference, and the area for the radii 0.5, 1.0, 1.5, . . ., 10.0.

Demo

// circle.cpp 
// Defines and calls the function circle(). 
#include <iostream> 
#include <iomanip> 
#include <string> 
using namespace std; 

// Prototype of circle(): 
void circle( const double& rad, double& um, double& fl); 

const double startRadius =  0.5,     // Start, end and 
               endRadius   = 10.0,     // step width of 
               step        =  0.5;     // the table 

int main() //from www.jav  a2 s .com
{ 
     double rad, circuit, plane; 
     cout << setw(10) << "Radius" 
           << setw(20) << "Circumference" 
           << setw(20) << "Area\n" << endl; 

     cout << fixed;           // Floating point presentation 
     for( rad = startRadius; rad < endRadius + step/2;  rad += step) 
     { 
       circle( rad, circuit, plane); 
       cout << setprecision(1)
            << setw(8)  
            << rad 
            << setprecision(5)
            << setw(22) 
            << circuit 
            << setw(20) 
            << plane <<endl; 
     } 
     return 0; 
} 
// Function circle(): Compute circumference and area. 
void circle( const double& r, double& u, double& f) 
{ 
    const double pi = 3.1415926536; 
    u = 2 * pi * r; 
    f = pi * r * r; 
}

Result

Related Exercise