Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to calculate the area of circle

C Code Example — Basic Programs

ADVERTISEMENT

C Program to calculate the area of circle

Objective

Write a C program to calculate the area of a circle.

Algorithm / Approach

  1. Declare a float variable radius and area.
  2. Declare a constant float pi = 3.14f.
  3. Read the radius from the user using scanf("%f", &radius).
  4. Calculate the area using the mathematical formula: area = pi * radius * radius.
  5. Print the result using printf("%f", area).
main.c
#include<stdio.h>
int main( )
{
 float radius, area;
 const float pi = 3.14f;
 printf("Enter radius: ");
 scanf("%f",&radius);
 area = pi*radius*radius;
 printf("CIRCLE DETAILS:\n");
 printf("RADIUS:%f\n",radius);
 printf("AREA:%f\n",area);
 return 0;
}

Expected Output

Enter radius : 8.2
CIRCLE DETAILS:
RADIUS:8.2
AREA :211.1336

Explanation of the Program

  • Because the radius and area can have decimal points, we use the float data type instead of int.
  • The const keyword is used to declare pi. This ensures that the value of pi remains constant throughout the program and cannot be accidentally modified.
  • The format specifier for floats in printf and scanf is %f.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT