Area of a Circle in C, C++, Java and Python
Step-by-step programs to find the area of a circle in C, C++, Java and Python, with sample output and common coding-round pitfalls.
The formula for the area of a circle is A = π × r², and coding it correctly means picking the right value of π and handling float precision in the language you’re writing in.
Placement coding rounds use this problem not to test geometry but to check that you know how to declare float variables, call a math library constant, and format decimal output. All four programs below accept a radius as input and print the area rounded to 2 decimal places.
The Formula and Float Precision
The exact value of π is irrational. Every programming language approximates it. The difference between 3.14 (two decimal digits) and 3.14159265358979 (fifteen decimal digits) is small in absolute terms, but placement tests often check output to the second decimal place. For a radius of 5, the area using 3.14 comes out as 78.50, while math.pi gives 78.54. A test expecting 78.54 will mark 78.50 wrong.
The safe practice is to use the language’s built-in constant wherever available. Python provides math.pi through its standard library. Java provides Math.PI as a constant in the java.lang package, which requires no import. C++ provides M_PI through the <cmath> header, though its availability without a compiler flag varies by platform. In plain C, the convention is to define your own constant with #define PI 3.14159 or #define PI 3.141593, which gives enough precision for placement-round test cases.
The output format matters as much as the constant. Most problems specify 2 decimal places. Using %f alone in C prints six decimal places by default. The required idiom is %.2f in C and C++, System.out.printf("%.2f") in Java, and :.2f in a Python f-string.
C Program for Area of a Circle
C supports three common styles for this problem, and some problem sets specify which one they want.
Basic approach
The simplest version reads the radius, computes the area with a #define constant, and prints the result:
#include <stdio.h>
#define PI 3.14159
int main() {
float r, area;
scanf("%f", &r);
area = PI * r * r;
printf("%.2f\n", area);
return 0;
}
- Sample input:
7 - Sample output:
153.94
The %.2f format specifier tells printf to print a float with exactly 2 digits after the decimal point. Writing %f alone prints six decimal places, which fails any test checking for exactly 2.
Function-based approach
Wrapping the calculation in a named function makes the logic easier to test in isolation:
#include <stdio.h>
#define PI 3.14159
float circleArea(float r) {
return PI * r * r;
}
int main() {
float r;
scanf("%f", &r);
printf("%.2f\n", circleArea(r));
return 0;
}
The function takes the radius as a parameter, computes the area, and returns a float. The main function handles all I/O. This separation keeps the computation logic free of any I/O dependency, which is the pattern interviewers reward in function-design questions.
Pointer-based approach
Some problem sets ask for a void function that modifies a variable through a pointer rather than returning a value:
#include <stdio.h>
#define PI 3.14159
void computeArea(float *r, float *area) {
*area = PI * (*r) * (*r);
}
int main() {
float r, area;
scanf("%f", &r);
computeArea(&r, &area);
printf("%.2f\n", area);
return 0;
}
The line *area = PI * (*r) * (*r) dereferences the radius pointer to read the value, computes the product, and writes the result back through the area pointer. Both &r and &area are passed to the function, so the caller retains ownership of both variables.
C++ Program for Area of a Circle
C++ can use M_PI from <cmath> for higher built-in precision. The output idiom also differs from C: pairing cout << fixed with cout.precision(2) sets fixed-point notation with 2 decimal places.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double r;
cin >> r;
double area = M_PI * r * r;
cout << fixed;
cout.precision(2);
cout << area << endl;
return 0;
}
- Sample input:
7 - Sample output:
153.94
Writing cout.precision(2) without cout << fixed is a common error. Without the fixed flag, precision(2) controls the total number of significant digits, not decimal places. Always pair them together.
On compilers where M_PI is not defined by default, a portable fallback is const double PI = 3.14159265; declared before main.
Java Program for Area of a Circle
Java’s Math.PI constant carries 15 significant digits and lives in java.lang, so no import statement is needed for it. The Scanner class handles input, and System.out.printf handles formatted output.
import java.util.Scanner;
public class AreaOfCircle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double r = sc.nextDouble();
double area = Math.PI * r * r;
System.out.printf("%.2f%n", area);
}
}
- Sample input:
7 - Sample output:
153.94
The %n in printf is the platform-independent newline; \n also works in most environments. The %.2f format specifier rounds the double to exactly 2 decimal places.
Python Program for Area of a Circle
Python gives the cleanest syntax for this calculation. The math module provides math.pi with full float precision, and f-string formatting handles the 2-decimal output in a single line:
import math
r = float(input())
area = math.pi * r * r
print(f"{area:.2f}")
- Sample input:
7 - Sample output:
153.94
Two details matter here. First, input() always returns a string; calling float() on it is required, or the multiplication raises a TypeError. Second, the f-string format :.2f rounds to 2 decimal places. Omitting the .2 and writing {area:f} prints 6 decimal places by default.
Common Mistakes in Coding Rounds
These errors appear repeatedly in placement submissions. Each costs marks because the logic is right but the output format or precision is wrong.
-
Wrong precision constant. Using 3.14 instead of 3.14159 shifts the second decimal digit for certain inputs. For a radius of 10, the area using 3.14 is 314.00; with
math.piit is 314.16. A test checking for 314.16 rejects 314.00. -
Integer division when the input is diameter. Some problems give diameter
dinstead of radiusr. The expression(d/2)in C performs integer division. For d=7, this evaluates to 3, not 3.5. The area comes out as PI × 3 × 3 = 28.27 instead of PI × 3.5 × 3.5 = 38.48. The fix: cast to float before dividing with(float)d / 2, or declaredas afloatfrom the start. Legacy programs on this problem frequently carried this bug when diameter was passed as a command-line argument. -
Missing
fixedflag in C++ output. Writingcout.precision(2)withoutcout << fixedcontrols significant digits, not decimal places. Pair both flags together. -
Forgetting
float()in Python. Theinput()function returns a string. Multiplying a string bymath.piraises aTypeErrorat runtime. Always convert before arithmetic. -
Wrong format specifier in C. Writing
printf("%d", area)for a float variable prints garbage output. Use%ffor the default 6 decimal places, or%.2fwhen 2 decimal places are required.
Where This Fits in Placement Tests
Area-of-circle questions appear in the basic coding section of campus placement assessments, typically as the first or second problem in a 3-problem set. The skill being tested is not geometry. It is float variable declaration, math library usage, and output format control. Every coding test checks all three, across every language.
Solving 10 to 15 problems in this category, covering basic math computations, array traversals, and string checks, builds the formatting precision and response speed that keeps you from losing time on simple problems when the harder ones in the same set actually need your full attention. Similar problems that follow the same input-output pattern include finding the smallest and largest element in an array and checking whether a string is a palindrome. For more in this style, see the FACE Prep collection of math-based programming problems.
Writing area = math.pi * r * r in Python trains the same instinct you need for numerical computation more broadly: state a formula, control float precision, format the result. TinkerLLM is where to extend that instinct toward LLM experiments, starting at ₹299.
Primary sources
Frequently asked questions
What is the formula for area of a circle?
Area equals pi times r squared, where r is the radius. In code, use math.pi in Python, Math.PI in Java, or a defined constant in C and C++.
Which value of PI should I use in a C program?
For most placement tests, defining PI as 3.14159 is sufficient. For higher precision, include math.h and use the M_PI constant.
Why does integer division give the wrong area when input is diameter?
In C, dividing two integers truncates the decimal. For diameter d=7, the expression d/2 equals 3, not 3.5. Cast to float first: use (float)d/2.
How do I print the area to exactly 2 decimal places?
In C and C++, use printf with the %.2f format specifier. In Java, use System.out.printf with %.2f. In Python, use an f-string with the :.2f format.
Is the area of a circle question common in placement coding rounds?
Yes, it appears in online assessments as a warm-up or output-formatting check. The key skills tested are float precision and correct use of the pi constant.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)