Circle Circumference
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
Zero radius
2*PI*1
2*PI*7
2*PI*5
2*PI*10
Algorithm Flow

Solution Approach
Multiply 2, pi, and the radius together using the language's pi constant.
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
class Solution {
public double solution(int r) {
return 2*Math.PI*r;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
