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 |
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:
- area of circle = Î R 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- area = 3.14 * radius * radius
- 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.
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:- Using #define Pre processor Directive: #define PI 3.14
- 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();
}
0 comments:
Post a Comment
We Love To Hear From You!