Hi this is anil from Learning Lad and in this tutorial i'm gonna teach you how to calculate
the area and circumference of a circle in the c programming language.
So we are gonna be writing a program which is gonna calculate the area and the circumference
of a circle.
So here i'm using the CodeBlocks IDE and if you guys don't know know you know how to download,
install and setup codeblocks ide then please watch my tutorial on it.
And here we are gonna be creating a new project.
So i'm gonna ggo to File, New and then Project.
And here we are gonna select the console application, and we are gonna click on this Go.And then
click on next.And I'm gonna select C because we are gonna be writing the program in c.
And then click next and we need to give the project title.
Lets say Area and Circumference of circle.
And then click on next.And click on Finish.
And now go to view and click on this manager.And its gonna open up the projects in your workspace
. And you guys see the project that i have created right now.
So i just gonna click on this sources and we are gonna double click on this main.c and
you guys can see it has some pre written code.
So i just gonna remove this printf statement and you know we are ready to go.
So in this tutorial we are gonna assume that that the radius of the circle is given so
to calculate the area of the circle we are gonna use the formula PI R square, you know
where R is the radius of the circle and then to calculate the circumference we
are gonna be using the formula 2 PI R and again R is the radius of the circle.
So here we are gonna need some variables to store the values of you know the radius, the
area , the circumference of the circle.
So we are gonna create the variables and they are gonna be of type float.
You know i just gonna take the float variables, because we are gonna be having some floating
point values.
So lets say area, and then we are gonna have the circumference, and let me call it as cf
short one, and then we are gonna have the radius, and the we are gonna have a constant
called PI which is equal to 3.14.
And then lets add the semicolon here.
Now we need a comma here you know after this radius, because you know we are defining more
than one variable here.
And the we are gonna ask the user to enter the radius of the circle.
So i'm gonna use the printf function, and i just gonna say enter the radius of the circle.
And lets add a new line here you know just to make it pretty.
And the we are gonna receive the value entered by the user and we are gonna store that in
our variable radius.
So i'm gonna use the scanf function.
And we are gonna be receiving a floating point value so i'm gonna use the format specifier
as %f and then we are gonna store that in our radius variable.
And the next thing that we are gonna do is we are gonna calculate the area of the circle.
So as i told you before you know we are gonna be using the formula PI R square where R is
the radius of the circle to calculate the area.
So we are gonna write area equals to PI which we have here the variable, and then multiplication,
we are gonna multiply it with he radius,and again we are gonna have the radius.
So its gonna be PI then radius two times and that's gonna give the area of the circle.
And the next thing that we are gonna do is we are gonna calculate the circumference of
the circle and and we are gonna store that in our cf variable.
So its gonna be cf equal to, we are gonna be using the formula 2 PI R where R is the
radius , and its gonna be 2 then multiply it with the value of PI and then we are gnna
multiply that with the value of radius.
You know these are the standard formula's for you know calcultaing the area and the
circumference of the circle.
The next thing that we are gonna do is we are gonna print out the values of this area
and circumference of the circle you know which we have calculated.
So i'm gonna use the printf function again.
So its gonna be printf and i'm gonna say Area of the circle is, and its gonna be %f because
you know we are talking about the float variable and lets add a newline just to make it pretty,
and then we are gonna refer the area variable.
So its gonna print out the value of the area, and then i just gonna copy it, you know this
whole printf statement, we are gonna paste it and i just gonna say circumference of the
circle is %f, and we are gonna refer cf you know the variable which holds the value of
the circumference . So the last thing that we are gonna do is
you know we are gonna run this program, so i'm gonna go to build, build and run and now
you guys can see its gonna ask us enter the radius of the circle.
Lets say the radius of the circle is 3.
And i'm gonna hit enter.
And it has calculated the area and the circumference.
And its gonna say area of the circle is 28.26 then the circumference of the circle is 18.84.
So similarly you know if i run this program again,you know i just gonna give the value
4, hit enter,and you know its gonna calculate the area and circumference of the circle for
the appropriate radius value.
So this is it guys.
This is how you guys can write a program in c to calculate area and circumference of the
circle.
Thank you for watching don't forget to subscribe to my channel.
Check out my website for the source code of this tutorial.