Find Area, Circumference and Diameter of Circle Using C++ in One Click

in #hive-1693212 years ago

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.

img_0.30176873933928544

Image Design By Canva

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;
}


img_0.9667936922421659

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.

img_0.2422654913920872

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.


img_0.416445580127289

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;
}


img_0.07024757276360254

It is the code for finding the circumference of the circle written in the compiler.

Output

img_0.2422654913920872

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.


img_0.18431309995152406

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;
}


img_0.32392082543061423

Here is the source code of the above diameter finding program written in the compiler.

Output

img_0.2422654913920872

So it is again has the same working and process.

img_0.9938492700754616

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;
}

img_0.7444093624555154

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

img_0.2422654913920872

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.

img_0.2035765675169166

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


All the images are taken from the Coding c++, a mobile application.


Sort:  

Nice one, I'm also planning to learn programming and starting with easy things would be much better for anyone

Personally never tried an android application but we've many to learn basic stuff without any hurdle or hardship!

Thanks for sharing, very appreciated.

I am also sharing the basic things so that a beginner can also learn and get an idea.

Wow man! Impressive.

Yeah ☺️

Great work!
I was actually wondering why you chose C++ of all the programming languages.
I mean you could also do this in Java or C# or Javascript but maybe you are comfortable with C++. 😅

Thank you dear for stopping by here.

Haha 🤣 exactly I feel myself more comfortable in c++

I see! If you feel comfortable with it, that’s fine 😅

Yeah 😌

I almost forgot about this C++ language. In my intermediate time i used to practice it .

But in current time it's really hard to remember everything. But with practice i can improve again 🙂.

Yes practice makes a man perfect and with practice you can do it.

This post has been manually curated by @bhattg from Indiaunited community. Join us on our Discord Server.

Do you know that you can earn a passive income by delegating to @indiaunited. We share 100 % of the curation rewards with the delegators.

Here are some handy links for delegations: 100HP, 250HP, 500HP, 1000HP.

Read our latest announcement post to get more information.

image.png

Please contribute to the community by upvoting this comment and posts made by @indiaunited.