How to Find Area of circle using Pointers in C

In this article, we are going to learn How to Find Area of circle using Pointers in C with examples.The formula to calculate the area of a circle is ℼ*r2 where r is the radius of the Circle.

C Program to find Area of Circle Using Pointers


In this example, we will write a C Program to find Area of Circle Using Pointers. We are asking the user to input the radius of the circle and we use it to find the area of a circle

#include<stdio.h>
#define PI 3.14

int main()
{
  float radius, Area;
  float *Radiusptr, *Areaptr;

  Radiusptr= &radius;
  Areaptr=&Area;

  printf("Please enter the radius of Circle: ");
  scanf("%f",Radiusptr);

  *Areaptr = PI*(*Radiusptr)*(*Radiusptr);

  printf("The Area of Cicle with radius %.3f = %.3f\n", *Radiusptr, *Areaptr);

  return 0;
}

Output

Please enter the radius of Circle: 5
The Area of Cicle with radius 5.000 = 78.500

2. Find Area of Circle Using Pointer & Function in C language


In this example, we are modifying the above program and moving the area calculation logic to a separate function.
We will take the input radius from the user and then calculate the area in a function. This function will return the area by using a pointer.

#include<stdio.h>
#define PI 3.14
int calculatearea(float *r, float *area);
int main()
{
  float radius, Area;
  float *Radiusptr, *Areaptr;

  Radiusptr= &radius;
  Areaptr=&Area;

  printf("Please enter the radius of Circle: ");
  scanf("%f",Radiusptr);

  calculatearea(Radiusptr, Areaptr);

  printf("The Area of Cicle with radius %.3f = %.3f\n", *Radiusptr, *Areaptr);

  return 0;
}
int calculatearea(float *r, float *area)
{
  *area = PI * *r * *r;
}

Output

Please enter the radius of Circle: 5
The Area of Cicle with radius 5.000 = 78.500