C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Friday, March 23, 2018

C++ Program To Calculate Area and Circumference of Circle in C Plus Plus Program

Easyway How To Write a Program in C++ for Calculating Area and Circumference of Circle

Program Statement: Write a C++ program to input radius of a circle and calculates and prints the area and circumference of the given circle.
Calculate area and circumference of circle in C++ Program Easyway
Calculate area and circumference of circle in C++ Program Easyway


What are the Formulas Used in C++ Program for Area and Circumference of Circle?

To write this program, first of all we have to find the accurate formulas for area and circumference of circle.
These are as follows:
  1. area of circle = Î  R 2
  2. circumference of circle =  2 Π R

How Formulas of Circle are Transformed into C++ Arithmetic Expressions

In C++ programming, we write the above formulas as follows
  1. area = 3.14 * radius * radius
  2. circum = 2 * 3.14 * radius
Note: 3.14 is the vale of constant PI  shown with symbol Π

A Better Programming Approach is to Use Symbolic Constants instead of hard coding numbers directly in C++ programs.

How To Define a Constant PI in C++ (C++ Program 

for Calculating Area and Circumference of  Circle

There are two ways:
  1. Using #define Pre processor Directive:     #define PI  3.14
  2. Using const Qualifier :                             const float PI = 3.14;

C++ Source Code for Calculating Area and Circumference of  Circle

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#define PI 3.14
void main()
{

   float radius, area, circum;
   clrscr();
    cout<<"Enter radius of circle = ";
    cin>>radius;
    area = PI * radius * radius;
    circum = 2 * PI * radius;
   
    cout<<"Area of  circle = "<<setprecision(2)<<area<<endl;
    cout<<"Circumference of circle = "<<setprecision(2)<<circum;

    getch();
}

How This Program Works for Calculating Area and Circumference of  Circle

#include<iostream.h>  // include header file for cin, cout
#include<conio.h>     // include header file for  clrscr() 
#include<iomanip.h>  // include header file for setprecision()
#define PI 3.14   // define constant PI
void main()
{

   float radius, area, circum;   declare variables
   clrscr();
    cout<<"Enter radius of circle = ";
    cin>>radius;           //input radius
    area = PI * radius * radius;   // calculate area
    circum = 2 * PI * radius;   // calculate circumference
   
   // used setprecision(2) to show value of area upto two decimal places
   cout<<"Area of  circle = "<<setprecision(2)<<area<<endl;

   // used setprecision(2) to show value of circumference upto two decimal places
    cout<<"Circumference of circle = "<<setprecision(2)<<circum;

    getch();
}


Share:

0 comments:

Post a Comment

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Labels