1. Introduction
Finding the area of geometric shapes is a fundamental concept in mathematics. For a circle, the area is calculated using the formula: area = π * radius^2. In this article, we'll be creating a JavaScript program to compute the area of a circle given its radius.
2. Program Overview
This guide will take you through:
1. Specifying the radius of the circle.
2. Constructing a function to determine the area of the circle based on its radius.
3. Displaying the computed area.
3. Code Program
let radius = 5; // Radius of the circle
let area; // Variable to hold the area of the circle
// Function to compute the area of a circle
function findArea(r) {
return Math.PI * r * r;
}
area = findArea(radius);
console.log("The area of a circle with radius " + radius + " units is: " + area.toFixed(2) + " square units.");
Output:
The area of a circle with radius 5 units is: 78.54 square units.
4. Step By Step Explanation
1. Variable Initialization: We initiate by designating the radius for which we want to compute the area. We also declare an area variable to store the result.
2. Area Calculation Function: The findArea(r) function performs the area computation. It uses the constant Math.PI to obtain the value of π (Pi) and then apply the formula for the area of a circle.
3. Computing the Area: We invoke our findArea function, passing in the defined radius, and save the outcome in the area variable.
4. Displaying the Result: We use console.log to output the radius and its corresponding circle area. We apply the toFixed(2) method to format the area, ensuring it's displayed with two decimal places for precision.
Comments
Post a Comment
Leave Comment