Code Logo

Circle Circumference

Published at25 Jul 2026
Geometry Easy 2 views
Like0

Calculate the circumference of a circle given its radius r. The formula is C = 2 × π × r. Use the built-in pi constant from your language's math library.

For example, a circle with radius 1 has circumference 2 × π × 1 = 2π ≈ 6.283. A circle with radius 5 has circumference 2 × π × 5 = 10π ≈ 31.416. If the radius is 0, the circumference is 0. The radius is a non-negative integer.

The circumference is the distance around the circle — essentially the circle's perimeter. This formula is used in countless real-world applications including wheel rotations (distance traveled per revolution), pipe bending, orbital mechanics, and circular track lengths.

In programming terms, you multiply the constant pi by 2 and by the radius. Most languages provide pi as a built-in constant: Math.PI in JavaScript and Java, math.pi in Python, pi() in PHP, and std::f64::consts::PI in Rust. Using the built-in constant guarantees the highest available floating-point precision.

The result is a floating-point number. The judge compares your answer against the expected value using a tolerance of 1e-9, so minor rounding differences from floating-point arithmetic are acceptable. Ensure that 2 * pi * r uses floating-point multiplication throughout to avoid integer truncation.

Example Input & Output

Example 1
Input
0
Output
0
Explanation

Zero radius

Example 2
Input
1
Output
6.283185307179586
Explanation

2*PI*1

Example 3
Input
7
Output
43.982297150257105
Explanation

2*PI*7

Example 4
Input
5
Output
31.41592653589793
Explanation

2*PI*5

Example 5
Input
10
Output
62.83185307179586
Explanation

2*PI*10

Algorithm Flow

Recommendation Algorithm Flow for Circle Circumference
Recommendation Algorithm Flow for Circle Circumference

Solution Approach

Multiply 2, pi, and the radius together using the language's pi constant.

function solution(r) {
  return 2 * Math.PI * r;
}

The formula C = 2πr translates directly to 2 * pi * r. Since both the integer 2 and the integer r are multiplied with the floating-point pi, the result is a float in all supported languages. The order of operations is left to right: (2 * pi) * r = (2π) * r = 2πr.

If the radius is 0, the multiplication yields 0.0 regardless of pi's value, correctly giving a circumference of zero. The built-in pi constant provides roughly 15-16 decimal digits of precision.

Time complexity is O(1) and space complexity is O(1).

Best Answers

java
class Solution {
    public double solution(int r) {
        return 2*Math.PI*r;
    }
}