Hello everyone! I hope you all are well and good and will be enjoying nature.
Today I am going to tell you about the basic program through which you can find the area and circumference of the circle.
I will teach you using c++ because it is simple to learn and I hope if you learn these simple things then you will be able to learn more complex things to perform in the programming.
How to find the area of the circle using c++?
First of all we write the header files and the library of the c++
#include< iostream >
using namespace std;
So these are the header files which are almost part of each program.
Now it is the time to declare the variables in which data is stored. There are different specific data types which are used according to their use.
As we have to deal with numbers so we can use int and float
data types. But still we have to filter these data types because the area of a circle can be in decimal form so we will use float
data type so that we can get an accurate value.
float r;
float area;
And now it is the next point to get a radius. We can simply set any fixed value to the radius r, as here the variable r is representing the radius.
r= 5;
But I would like to make a program that can accept any value of the radius given by the user. Because this program can use anyone but if I fix the value of radius then it is of no use to set the fix value.
How to get input value from the user?
Firstly we will print a simple message to display that we want to get the value of the radius.
cout<<"Enter Radius";
cin>>r;
After that in the program we have to write the formula of the area of the circle. As we know that area of the circle is given by: πr2. As I have declared variable area to store the value of area so I am putting formula of area in that.
We put the value of π in the formula which is approximately equal to 3.14159. And similarly in the programming we can't use direct squares of any value so we multiply the radius r * r
area = 3.14159 * r * r;
So now I am going to compile the whole program using c++ and then in the next section I will compile this program using JavaScript.
All the code lines are terminated by the semicolon in the c++, otherwise the program will give an error and will not execute.
Area of Circle Using C++
#include< iostream >
using namespace std;
int main ( )
{
float r, area;
cout<<"Enter The Value of Radius = ";
cin>>r;
area=3.14159 * r * r;
cout<<"The Area of the circle of the given radius = "<<area<<" sq.units";
return 0;
}
It is the above input code in the complier.
Output
It's output and working is simple as you can see in the code and we have discussed it earlier that we will get input value of the radius from the user. So when we run this program it will start executiing and firstly it will ask to input the value of the radius as you can see below.
So now after entering the value of the radius the next program will execute and it will give the value of the radius of the circle.
So here you can see I put the value of the radius 5 and it has given the output 78.5397sq.units which is the area of the circle.
Circumference of Circle Using C++
This program is similar to the above we just have to change the formula. As the formula of the circumference of the circle is 2πr, let me show you how it will work.
#include< iostream >
using namespace std;
int main ( )
{
float r, circumference;
cout<<"Enter The Value of Radius = ";
cin>>r;
circumference= 2 * 3.14159 * r;
cout<<"The Circumference of the circle of the given radius = "<<circumference<<" units";
return 0;
}
It is the code for finding the circumference of the circle written in the compiler.
Output
Here you can see the procedure and working of this program and it is the same as the above program. So here we have to input the value of radius and then the program will be processed next.
So here you can see that I again gave 5 as an input value of radius and it has given 31.4159 units output as the circumference of the circle.
Diameter of Circle Using C++
It is also a simple input output program related to the last two programs. But we just have to change the formula. As the formula of diameter is 2 * r, so now it's time to write the code.
#include< iostream >
using namespace std;
int main ( )
{
float r, diameter;
cout<<"Enter The Value of Radius = ";
cin>>r;
diameter = 2 * r;
cout<<"The Diameter of the circle of the given radius = "<<diameter" units";
return 0;
}
Here is the source code of the above diameter finding program written in the compiler.
Output
So it is again has the same working and process.
So again I have put 5 as value of the radius and you can see the compiler result 10 units which is the value of the diameter of the circle having radius 5.
Find Area, Circumference and Diameter of the circle using C++
Now I am going to write a program through which we can find the area, circumference and diameter of the circle with one click. After entering the radius all the values will be in your hand. So let's see how it works.
#include< iostream >
using namespace std;
int main ( )
{
float r, area, circumference, diameter;
cout<<"Enter The Value of Radius = ";
cin>>r;
area=3.14159 * r * r;
circumference= 2 * 3.14159 * r;
diameter = 2 * r;
cout<<"The Radius of the circle of the given radius = "<<area<<"sq.units";
cout<<"The Circumference of the circle of the given radius = "<<circumference<<"units";
cout<<"The Diameter of the circle of the given radius = "<<diameter" units";
return 0;
}
So here is the code of all the solutions, from this code you can fund the area, circumference and diameter of the circle in one click. You can see the code written in the compiler.
Output
Here is the magic step, after entering the value of the radius you will get the three values from one single program. So let's see.
So finally output is here and you can see we have to enter radius only and we can get the area, circumference and diameter of the that circle.
So if you want to find area, circumference and diameter of the circle and want to write the program for these things then you can write in this way using c++ language.
Yours: @uop