Program to find area and circumference |HTML|

Introduction

    Are you looking to quickly calculate the area and circumference of a circle without leaving your browser? With JavaScript, you can create a simple web form that allows users to input the radius of a circle and instantly receive the area and circumference. In this blog post, we'll guide you through building a web form for this purpose.

JavaScript functions are reusable blocks of code that perform a specific task. In our case, we've defined two functions: area(r) to calculate the area of a circle and circumference(r) to calculate the circumference.

Code

    
     
      <html>
      <head>
          <title>Circle Calculator  </title>
          <script type="text/javascript">
            // JavaScript functions to calculate area and circumference
            function area(r) {
                return 3.14 * r * r;
            }
            
            function circumference(r) {
                return 2 * 3.14 * r;
            }
          </script>
      </head>
      <body>
          <form name="frmcircle">
              <p align="center">
                Enter the radius:   <input type="text" name="textradius" />
                  <br>
                  <input type="submit" value="Calculate Area" onClick="alert('Area: ' + area(document.frmcircle.textradius.value))" />
                  <input type="submit" value="Calculate Circumference" onClick="alert('Circumference: ' + circumference(document.frmcircle.textradius.value))" />
              </p>
          </form>
      </body>
      </html>
    
       
     


Output








Explanation

  • We've created a web form with an input field for users to enter the radius of the circle.
  • Two submit buttons are provided: one for calculating the area and another for calculating the circumference.
  • When a button is clicked, the corresponding JavaScript function is called with the radius value from the input field, and an alert message displays the result.

Conclusion

     With this simple web form, users can effortlessly calculate the area and circumference of a circle by providing the radius. JavaScript functions handle the calculations behind the scenes, providing instant feedback to the user. This can be a handy tool for quick calculations or educational purposes. Feel free to customize the design or add additional functionality to suit your needs.

Post a Comment

0 Comments