React Js Calculate Area & Perimeter of Circle
React Js Calculate Area & Perimeter of Circle:In React.js, you can calculate the area and perimeter of a circle by taking user input for the radius, performing the necessary mathematical calculations, and displaying the results. First, capture the radius value from the user through a form input. Then, use React's state management to store and update the radius value. Next, calculate the area (π * r^2) and perimeter (2 * π * r) using JavaScript functions. Finally, render these values on the screen, updating them whenever the radius input changes. This interactive approach allows users to input different radius values and instantly see the corresponding area and perimeter of the circle in a React application.
![Profile Photo](https://fontawesomeicons.com/images/abhishek.png)
![Profile Photo](https://fontawesomeicons.com/images/anil.png)
![Profile Photo](https://fontawesomeicons.com/images/calendar2.png)
![Feedback Image](https://www.sarkarinaukriexams.com/images/editor/1709103057contribution.png)
Thanks for your feedback!
Your contributions will help us to improve service.
How to calculate the area of a circle in a Reactjs web app?
This React.js code defines a simple circle area calculator. It utilizes React state to manage user input and display the calculated result. The user enters the radius of a circle, clicks the "Calculate" button, and the app computes the area using the formula πr^2, where 'r' is the radius. It validates user input for numeric values and handles various error cases, displaying appropriate messages. The result is presented with two decimal places. The app's interface includes a title, an input field, and a button. Upon calculation, it shows the area result
React Js Calculate Area of Circle Example 1
<script type="text/babel">
const { useState } = React;
function App() {
const [inputValue, setInputValue] = useState('');
const [result, setResult] = useState('');
const calculateArea = () => {
if (inputValue) {
const parsedRadius = parseFloat(inputValue);
if (!isNaN(parsedRadius)) {
const circleArea = Math.PI * Math.pow(parsedRadius, 2);
setResult(`The area of the circle with a radius of ${inputValue} units is ${circleArea.toFixed(2)} square units.`);
} else {
setResult('Invalid input');
}
} else {
setResult('Please enter a radius');
}
};
return (
<div className='container'>
<h1 className='title'>React Js Circle Area Calculator</h1>
<label className='label'>
Enter the radius of the circle:
<input
type="number"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
className='input'
/>
</label>
<button onClick={calculateArea} className='button'>Calculate</button>
<div className='result'>
{result && <p className='result-text'>{result}</p>}
</div>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js Calculate Area & Perimeter of Circle
How can you use Reactjs to calculate the perimeter of a circle given its radius?
This React.js code defines a simple Circle Perimeter Calculator. It uses React state to manage a radius input field and display the calculated perimeter of a circle. Users can enter a numeric radius, and upon clicking the "Calculate Perimeter" button, the code validates the input, computes the perimeter using the formula 2 * π * radius, and displays the result rounded to two decimal places. If the input is invalid (not a number or empty), it shows "Invalid input." The app's UI includes a title, input field, button, and a paragraph to display the calculated perimeter. It utilizes React hooks and functional components for state management.
React Js Calculate Perimeter of Circle Example 2
<script type="text/babel">
const { useState } = React;
function App() {
const [radius, setRadius] = useState('');
const [perimeter, setPerimeter] = useState('');
const handleRadiusChange = (event) => {
setRadius(event.target.value);
};
const calculatePerimeter = () => {
if (!isNaN(radius) && radius !== '') {
const parsedRadius = parseFloat(radius);
const calculatedPerimeter = 2 * Math.PI * parsedRadius;
setPerimeter(calculatedPerimeter.toFixed(2)); // Round to 2 decimal places
} else {
setPerimeter('Invalid input');
}
};
return (
<div className='container'>
<h2 className='title'>React Js Circle Perimeter Calculator</h2>
<label className='label'>Enter the radius of the circle:</label>
<input
type="number"
value={radius}
onChange={handleRadiusChange}
placeholder="Enter radius"
className='input'
/>
<button onClick={calculatePerimeter} className='button'>
Calculate Perimeter
</button>
<p className='result'>Perimeter of the circle: {perimeter}</p>
</div>
)
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>