Computer Science, asked by chandelnishasingh, 7 days ago

write a Java program to calculate the area of circle formula: Area =πr2 where π = 22/7​

Answers

Answered by anindyaadhikari13
5

\texttt{\textsf{\large{\underline{Solution}:}}}

The given problem is solved using language - Java.

import java.util.*;

public class CircleArea{

   public static void main(String s[]){

       double r,a;

       Scanner sc=new Scanner(System.in);

       System.out.print("Enter radius of circle: ");

       r=sc.nextDouble();

       sc.close();

       a=Math.PI*r*r;

       a=Math.round(a*100)/100.0;

       System.out.println("Area of the circle is: "+a+" square unit.");

   }

}

\texttt{\textsf{\large{\underline{Explanation}:}}}

  • Line 1: Imports Scanner class to take input.
  • Line 2: Class declaration.
  • Line 3: Start of main().
  • Line 4: Declared two variables to store radius and area of the circle.
  • Line 5: Object of Scanner class created.
  • Line 6: Asks the user to enter the radius of the circle.
  • Line 7: The radius value is taken as input.
  • Line 8: Area of the circle is calculated. Math.PI contains the value of π.
  • Line 9: The area of the circle is rounded to two decimal places.
  • Line 10: The area is displayed on the screen.
  • Line 11: End of main().
  • Line 12: End of class.

See attachment for output.

Attachments:
Similar questions