C Program To Find Area and Circumference of Circle using Pointer


Lets write a C program to calculate area and circumference or perimeter of a Circle using pointer and function.

Related Read:
Function / Methods In C Programming Language
Basics of Pointers In C Programming Language

Video Tutorial: C Program To Find Area and Circumference of Circle using Pointer



YouTube Link: https://www.youtube.com/watch?v=ekmC25v6TqU [Watch the Video In Full Screen.]


Source Code: C Program To Find Area and Circumference of Circle using Pointer

  1. #include<stdio.h>  
  2.   
  3. void area_peri(floatfloat*, float*);  
  4.   
  5. int main()  
  6. {  
  7.     float radius, area, perimeter;  
  8.   
  9.     printf("Enter radius of Circle\n");  
  10.     scanf("%f", &radius);  
  11.   
  12.     area_peri(radius, &area, &perimeter);  
  13.   
  14.     printf("\nArea of Circle = %0.2f\n", area);  
  15.     printf("Perimeter of Circle = %0.2f\n", perimeter);  
  16.   
  17.     return 0;  
  18. }  
  19.   
  20. void area_peri(float r, float *a, float *p)  
  21. {  
  22.     *a = 3.14 * r * r;  
  23.     *p = 2 * 3.14 * r;  
  24. }  

Output 1:
Enter radius of Circle
5

Area of Circle = 78.50
Perimeter of Circle = 31.40

Output 2:
Enter radius of Circle
14

Area of Circle = 615.44
Perimeter of Circle = 87.92

Logic To Find Area and Circumference of Circle using Pointer

We ask the user to enter value for radius of a Circle. We pass this value along with address of variables area and perimeter to the function area_peri().

area_peri(radius, &area, &perimeter);

We copy the value of radius to a local variable r and then we take 2 floating point pointer variables *a and *p. *a represents the value present at address a or &area. *p has value present at address p or &perimeter.

Inside area_peri() function we calculate the area and circumference / perimeter of Circle and store it as value present at addresses a and p. Since a points to address of variable area and p points to address of variable perimeter, the values of variable area and perimeter changes too.

*a = 3.14 * r * r;

*p = 2 * 3.14 * r;

Area and Circumference of Circle

We’ve separate video tutorials to calculate area and circumference of a Circle using radius, and without using pointer and function. You can check them out at these links:

Calculate Area of a Circle without using math.h library: C
C Program To Calculate Circumference of Circle

Note: When * is precedes any address, it fetches the value present at that address or memory location.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *