Given equation of a circle as string, find area
Given an equation of the circle X2 + Y2 = R2 whose center at origin (0, 0) and the radius is R. The task is to find area of circle.
Examples :
Input : X*X + Y*Y = 25
Output : The area of circle centered at origin is : 78.55
Input : X*X + Y*Y = 64
Output : The area of circle centered at origin is : 201.088
Approach:
- Given an equation X2 + Y2 = R2 and store it into string ‘str’.
- Count length of string and store it into ‘len’.
- Start loop from 0 to len – 1 and check if str[i] == ‘=’.
- Store characters after ‘=’ into string variable st.
- Convert string ‘st’ into digits and store it into ‘radius_square’.
- Use formula Pi * R2 to find area of circle(multiply by Pi).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define PI 3.142
using namespace std;
double findArea( double radius)
{
return PI * pow (radius, 2);
}
static double findradius(string str)
{
double radius = 0;
string st = "" ;
int c = 0;
int len = str.length();
for ( int i = 0; i < len; i++) {
if (str[i] == '=' ) {
c = 1;
}
else if (c == 1 && str[i] != ' ' ) {
st += str[i];
}
}
radius = ( double ) sqrt (stoi(st));
return radius;
}
int main()
{
string str = "X*X + Y*Y = 100" ;
double radius = findradius(str);
cout << "The area of circle " <<
"centered at origin is : " <<
findArea(radius) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static double PI = 3.142 ;
static double findArea( double radius)
{
return PI * Math.pow(radius, 2 );
}
static double findradius(String str)
{
double radius = 0 ;
String st = "" ;
int c = 0 ;
int len = str.length();
for ( int i = 0 ; i < len; i++) {
if (str.charAt(i) == '=' ) {
c = 1 ;
}
else if (c == 1 && str.charAt(i) != ' ' )
{
st = st + str.charAt(i);
}
}
if (c == 1 )
radius = ( double )Math.sqrt(
Integer.parseInt(st));
return radius;
}
public static void main(String[] args)
{
String str = "X*X + Y*Y = 100" ;
double radius = findradius(str);
System.out.println( "The area of circle"
+ " centered at origin is : "
+ findArea(radius));
}
}
|
Python3
import math
def findArea(radius):
return math.pi * pow (radius, 2 )
def findradius( str ):
radius = 0
st = ""
c = 0
Len = len ( str )
for i in range ( 0 , Len ):
if ( str [i] = = '=' ):
c = 1
elif (c = = 1 and str [i] ! = ' ' ):
st = st + str [i]
radius = float (math.sqrt( float (st)))
return radius
str = "X*X + Y*Y = 100"
radius = findradius( str )
print ( "The area of circle " ,
"centered at origin is : " ,
(findArea(radius)))
|
C#
using System;
class GFG
{
static double findArea( double radius)
{
return Math.PI * Math.Pow(radius, 2);
}
static double findradius( string str)
{
double radius = 0;
String st = "" ;
int c = 0;
int len = str.Length;
for ( int i = 0; i < len; i++)
{
if (str[i] == '=' )
{
c = 1;
}
else if (c == 1 && str[i] != ' ' )
{
st = st + str[i];
}
}
if (c == 1)
radius = ( double )Math.Sqrt(
int .Parse(st));
return radius;
}
public static void Main()
{
string str = "X*X + Y*Y = 100" ;
double radius = findradius(str);
Console.WriteLine( "The area of circle" +
" centered at origin is : " +
System.Math.Round(findArea(radius),1));
}
}
|
Javascript
<script>
let PI = 3.142;
function findArea(radius)
{
return PI * Math.pow(radius, 2);
}
function findradius(str)
{
let radius = 0;
let st = "" ;
let c = 0;
let len = str.length;
for (let i = 0; i < len; i++) {
if (str[i] == '=' ) {
c = 1;
}
else if (c == 1 && str[i] != ' ' )
{
st = st + str[i];
}
}
if (c == 1)
radius = Math.sqrt(
parseInt(st));
return radius;
}
let str = "X*X + Y*Y = 100" ;
let radius = findradius(str);
document.write( "The area of circle"
+ " centered at origin is : "
+ findArea(radius));
</script>
|
Output:
The area of circle centered at origin is : 314.2
Time complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n), for storing the value of R2 in string str.
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...