Area of a Circle: flowchart, algorithm and program

This program reads the radius of a circle from the user and prints its area. It uses the formula area = 3.14159 × r × r and shows the answer rounded to 2 decimal places.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read r.
  3. Step 3: Set area = 3.14159 * r * r.
  4. Step 4: Print area.
  5. Step 5: Stop.

Pseudocode

BEGIN
    READ r
    area ← 3.14159 * r * r
    PRINT area
END

Program in C, Python and Java

Area of a Circle program in C

#include <stdio.h>

int main() {
    float r, area;
    printf("Enter radius: ");
    scanf("%f", &r);
    area = 3.14159 * r * r;
    printf("Area = %.2f\n", area);
    return 0;
}

Area of a Circle program in Python

r = float(input("Enter radius: "))
area = 3.14159 * r * r
print("Area = {:.2f}".format(area))

Area of a Circle program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter radius: ");
        double r = sc.nextDouble();
        double area = 3.14159 * r * r;
        System.out.printf("Area = %.2f%n", area);
        sc.close();
    }
}

Sample input: r = 5
Output: Area = 78.54

How it works

  • Two float variables are declared: r for the radius and area for the result. float is used because a radius like 2.5 is not a whole number.
  • scanf("%f", &r) reads the radius typed by the user and stores it in r. The & is needed so scanf can write into the variable.
  • The line area = 3.14159 * r * r; multiplies pi (approximated as 3.14159) by r twice, which is the same as pi times r squared.
  • printf("Area = %.2f\n", area) prints the result. The %.2f tells C to show only 2 digits after the decimal point.
  • Dry run with r = 5: area = 3.14159 * 5 * 5 = 78.53975, which prints as 78.54.

Dry run (trace table)

StepLineBoxrareaOutput
13Start
26Read r5Enter radius:
37area = 3.14159 * r * r578.53975
48Print area578.53975Area = 78.54
510Stop578.53975

Time and space complexity

Time: O(1) · Space: O(1). The program does a fixed number of operations and uses only two variables, regardless of input.

Common mistakes

  • Forgetting the & in scanf (writing scanf("%f", r)), which causes a crash or garbage value.
  • Using %d instead of %f to read or print a float, which gives wrong or strange output.
  • Trying to write r^2 for the square. In C, ^ is the XOR operator, not power, so use r * r instead.

Viva questions

Q1. What does this program do, and what formula does it use?

It reads a radius from the user and prints the area of the circle using area = 3.14159 * r * r, which is pi times radius squared.

Q2. Why did you declare r and area as float and not int?

A radius and an area can have decimal parts, like 2.5 or 19.63. An int would drop the fractional part and give wrong results.

Q3. Why do we write &r in scanf but just area in printf?

scanf needs the address of r so it can store the typed value there. printf only needs the value to print, so no address is passed.

Q4. What does %.2f do in the printf statement?

It prints the float rounded to 2 digits after the decimal point. For example, 19.6349 is shown as 19.63.

Q5. What will the program print if the radius is 0? What if it is negative, like -3?

For 0 it prints Area = 0.00. For -3 it prints 28.27, because r*r is positive. This is wrong for a real circle, since a radius can't be negative, so the program should check for r < 0 and show an error.

Q6. On line 7, 3.14159 is a double constant but area is a float. What happens?

r is promoted to double for the calculation, and the result is converted back to float when stored in area. It works, but some precision is lost. Writing 3.14159f or using double throughout would avoid the conversion.

Q7. What is the time complexity of this program?

It is O(1), constant time. It does a fixed number of operations (one read, two multiplications, one print) whatever the input value.

Q8. What would you change to get more accurate results or to use a better value of pi?

Use double for the variables with the %lf format in scanf, and use M_PI from math.h or #define PI 3.14159265358979. This gives more precision than float with 3.14159.

Frequently asked questions

Why do we multiply r twice instead of using r^2?

In C the ^ symbol means bitwise XOR, not power. Writing r * r gives the square correctly, and it is simpler than calling pow().

Why does the output show 78.54 when the exact value is 78.53975?

The format %.2f rounds the number to 2 decimal places, so 78.53975 is printed as 78.54.

Can I use a more accurate value of pi?

Yes. You can use 3.14159265 or the M_PI constant from math.h for better accuracy. This program uses 3.14159, which is enough for most exercises.

More programs

← All programs

Try AI Flowchart free →