Given a circle C1 and it’s a radius r1. And one another circle C2 whose passes through center of circle C1 and touch the circumference of circle C1. The task is to find out the area of circle C2.
Examples:
Input: r1 = 4
Output:Area of circle c2 = 12.56
Input: r1 = 7
Output:Area of circle c2 = 38.465
Approach:
Radius r2 of circle C2 is .
So we know that the area of circle is .
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include<bits/stdc++.h> #include <iostream> using namespace std; // Function calculate the area of the inner circle double innerCirclearea( double radius) { // the radius cannot be negative if (radius < 0) { return -1; } // area of the circle double r = radius / 2; double Area = (3.14 * pow (r, 2)); return Area; } // Driver Code int main() { double radius = 4; cout << ( "Area of circle c2 = " , innerCirclearea(radius)); return 0; } // This code is contributed by jit_t. |
Java
// Java implementation of the above approach class GFG { // Function calculate the area of the inner circle static double innerCirclearea( double radius) { // the radius cannot be negative if (radius < 0 ) { return - 1 ; } // area of the circle double r = radius / 2 ; double Area = ( 3.14 * Math.pow(r, 2 )); return Area; } // Driver Code public static void main(String arr[]) { double radius = 4 ; System.out.println( "Area of circle c2 = " + innerCirclearea(radius)); } } |
Python3
# Python3 implementation of the above approach # Function calculate the area of the inner circle def innerCirclearea(radius) : # the radius cannot be negative if (radius < 0 ) : return - 1 ; # area of the circle r = radius / 2 ; Area = ( 3.14 * pow (r, 2 )); return Area; # Driver Code if __name__ = = "__main__" : radius = 4 ; print ( "Area of circle c2 =" , innerCirclearea(radius)); # This code is contributed by AnkitRai01 |
C#
// C# Implementation of the above approach using System; class GFG { // Function calculate the area // of the inner circle static double innerCirclearea( double radius) { // the radius cannot be negative if (radius < 0) { return -1; } // area of the circle double r = radius / 2; double Area = (3.14 * Math.Pow(r, 2)); return Area; } // Driver Code public static void Main(String []arr) { double radius = 4; Console.WriteLine( "Area of circle c2 = " + innerCirclearea(radius)); } } // This code is contributed by PrinciRaj1992 |
Javascript
// Function to calculate the area of the inner circle function innerCircleArea(radius) { // The radius cannot be negative if (radius < 0) { return -1; } // Area of the circle const r = radius / 2; const area = 3.14 * Math.pow(r, 2); return area; } // Driver Code const radius = 4; console.log( "Area of circle c2 = " + innerCircleArea(radius)); |
12.56
Time Complexity : O(log r)
Auxiliary Space : O(1) ,as we are not using any extra space.
Approach 2:
- Calculate the diameter of circle C1 by multiplying the radius r1 by 2.
- Calculate the radius r2 of circle C2 by dividing the diameter of C1 by 2.
- Calculate the area of circle C2 using the formula: Area = π * r2^2.
Below is the implementation of the above approach:
C++
#include <cmath> #include <iomanip> #include <iostream> double calculateCircleArea( double radius) { if (radius < 0) { return -1; // Invalid radius } double radiusC2 = radius / 2; double areaC2 = 3.14 * pow (radiusC2, 2); return areaC2; } int main() { double radiusC1 = 4; double areaC2 = calculateCircleArea(radiusC1); std::cout << std::fixed << std::setprecision(2) << "Area of circle C2: " << areaC2 << std::endl; return 0; } |
Java
import java.util.Scanner; public class Main { // Function to calculate the area of the inner circle static double calculateCircleArea( double radius) { // The radius cannot be negative if (radius < 0 ) { return - 1 ; } // Calculate the radius of circle C2 double r = radius / 2 ; // Calculate the area of circle C2 double area = ( 3.14 * Math.pow(r, 2 )); return area; } public static void main(String[] args) { // Input radius double radiusC1 = 4 ; // Calculate and print the area of circle C2 System.out.println( "Area of circle C2: " + calculateCircleArea(radiusC1)); } } |
Python
import math def calculate_circle_area(radius): """ Calculate the area of a circle given its radius. Args: radius (float): The radius of the circle. Returns: float: The area of the circle. Returns -1 if the radius is invalid (negative). """ if radius < 0 : return - 1 # Invalid radius, return -1 as an error flag radius_c2 = radius / 2 area_c2 = 3.14 * math. pow (radius_c2, 2 ) return area_c2 def main(): """ Main function to demonstrate the calculation of the area of a circle. The radius (C1) is given as 4, and the area of the circle (C2) is calculated and displayed. """ radius_c1 = 4 area_c2 = calculate_circle_area(radius_c1) print ( "Area of circle C2: {:.2f}" . format (area_c2)) if __name__ = = "__main__" : main() |
C#
using System; class GFG { // Function to calculate the area of a circle given its // radius static double CalculateCircleArea( double radius) { if (radius < 0) { return -1; // Invalid radius } double radiusC2 = radius / 2; double areaC2 = 3.14 * Math.Pow(radiusC2, 2); return areaC2; } static void Main() { double radiusC1 = 4; double areaC2 = CalculateCircleArea(radiusC1); // Print the calculated area of circle C2 with 2 // decimal places Console.WriteLine( "Area of circle C2: " + areaC2.ToString( "F2" )); } } |
Javascript
// Function to calculate the area of a circle with a given radius function calculateCircleArea(radius) { if (radius < 0) { return -1; // Return -1 for invalid radius (negative radius) } // Calculate the radius of C2 (half of the given radius) const radiusC2 = radius / 2; // Calculate the area of circle C2 using the formula: area = π * r^2 // where π (pi) is approximately 3.14 const areaC2 = 3.14 * Math.pow(radiusC2, 2); return areaC2; // Return the calculated area } function main() { const radiusC1 = 4; // Given radius of circle C1 const areaC2 = calculateCircleArea(radiusC1); // Calculate the area of circle C2 using the given radius console.log(`Area of circle C2: ${areaC2.toFixed(2)}`); // Display the area of circle C2 with 2 decimal places return 0; // Return 0 to indicate successful execution (not necessary in JavaScript) } main(); // Call the main function to start the execution |
Area of circle C2: 12.56
Time Complexity : O(1)
Auxiliary Space : O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!