Sunday, 26 October 2014

find area of circle


Program to find area of circle

Let’s see how to write this C program

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float area,radius;
const float pi=22/7;
printf("enter the radius of circle ");
scanf("%f",&radius);

area=2*pi*pow(radius,2) ;
//area=2*pi*radius*radius;

printf("area of the circle is = %f",area);
getch();
return 0;
}


Explanation of the program


float area,radius;
Float is a kind of data type which is of size ‘4bytes’.
it is use store the decimal values or we can say real number.
we are declaring area and radius as the variable of float datatype.

const float pi=3.1428;
Here we are declaring a variable named as pi with the constant value of 3.1428.
Const is keyword which specify that variable pi is a constant so its value can’t be changed in run time of the program.

scanf("%f",&radius);
It is use to receive the the value for variable radius.
As we know that the radius is of datatype float so we need to use “%f” to store that float value.

area=2*pi*pow(radius,2);
This is a mathematical expression equivalent to ‘area=2*π*radius2 ‘.
We can also write it as     area=2*pi*radius*radius;

pow(radius,2)
pow() is a power function in the header file of math.h.
which accepts two parameters one is the number and another is the power of that number.
Eg:- if we want to write
A2  then we write it as pow(A,2);
A4  then we write it as pow(A,4);
2n  then we write it as pow(2,n);

printf("area of the circle is = %f",area);
This statement will display the “area of the circle is = “ followed by the value stored in the variable area.

%f
This tells the compiler that we are going to display or read the real number.

Getch(); and return 0;
We already discussed this click here to move back.


Pictures of program:

Step 1:  Create the program:-





Step 2: output 




Note:  if any of the programmer is facing the problem of repapering of the previous screen again and again

He can use the function clrscr();
It present in the conio.h header file
After declaring the variables

No comments:

Post a Comment