Write a C Program to Calculate Area and Circumference Using Function
1. Introduction
In this tutorial, we will learn how to write a simple C program to calculate the area and circumference of a circle using functions. This is a common beginner-level programming question asked in exams and viva.
2. Problem Statement
Question: b) Write a C program to calculate area and circumference using function.
The program should:
- Take the radius of the circle as input from the user
- Use separate functions to calculate area and circumference
- Display the results clearly
3. Logic
We use the formulas:
Area of Circle = π × r × r
Circumference of Circle = 2 × π × r
Steps:
- Declare function prototypes.
- Take radius as input.
- Call functions by passing radius.
- Print returned values.
4. Source Code (Beginner Friendly)
#include <stdio.h>
float calculateArea(float radius)
{
float area;
area = 3.14 * radius * radius;
return area;
}
float calculateCircumference(float radius)
{
float circumference;
circumference = 2 * 3.14 * radius;
return circumference;
}
int main()
{
float radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = calculateArea(radius);
circumference = calculateCircumference(radius);
printf("Area of the circle = %.2f\n", area);
printf("Circumference of the circle = %.2f\n", circumference);
return 0;
}
5. Sample Output
Enter the radius of the circle: 5
Area of the circle = 78.50
Circumference of the circle = 31.40
6. Viva Questions (With Answers)
- Q1. What is a function in C?
- Answer: A function in C is a block of code that performs a specific task and can be called whenever required.
- Q2. Why do we use functions in programs?
- Answer: Functions help in code reusability and make programs easier to understand.
- Q3. What is the formula for the area of a circle?
- Answer: Area = π × r × r.
- Q4. What is the return type of a function?
- Answer: It specifies the type of value returned by the function such as int or float.
- Q5. What is the difference between actual and formal parameters?
- Answer: Actual parameters are passed during function call, while formal parameters receive them in the function definition.
Conclusion
This program demonstrates the use of functions in C and is commonly asked in practical exams and viva.
