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 https://www.youtube.com/watch?v=ekmC25v6TqU]
Page Contents
Source Code: C Program To Find Area and Circumference of Circle using Pointer
#include<stdio.h> void area_peri(float, float*, float*); int main() { float radius, area, perimeter; printf("Enter radius of Circle\n"); scanf("%f", &radius); area_peri(radius, &area, &perimeter); printf("\nArea of Circle = %0.2f\n", area); printf("Perimeter of Circle = %0.2f\n", perimeter); return 0; } void area_peri(float r, float *a, float *p) { *a = 3.14 * r * r; *p = 2 * 3.14 * r; }
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