Area & Perimeter of Circle in Java | Java Practice Question – 2

https://youtu.be/S3KMDQl2F3s
Write a Java program that will take radius of a circle from user and, display the area and perimeter of the circle

Firstly, We have to import Java’s predefined Scanner Class from package java.util

import java.util.Scanner

Now, Begins the declaration of class RadiusOfCircle. and remember, the file name for this public class must be RadiusOfCircle.java

public class RadiusOfCircle

Note – Don’t forget an opening and closing braces.

Create the main method of java. that is available on every java program

public static void main(String[] args)

Note – Don’t forget an opening and closing braces.

Declaring and Creating a Scanner to Obtain User Input from the Keyboard.

Scanner input = new Scanner(System.in)

Declaring Variables to Store Float and Double

float radius;
double area, radius;

Prompting the User for input and Obtaining float as input from the user

System.out.print("Enter the Radius --> ");
radius = input.nextFloat();

Use the formula of Area ad Perimeter of a Circle ( Note – PI = 3.141)

area = 3.141 * radius * radius;
perimeter = 2 * 3.141 * radius;

Display the Output

System.out.printf("Area = %.2f and Perimeter = %.2f", area, perimeter);
import java.util.Scanner;
public class RadiusOfCircle
{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    float radius;
    double area, perimeter;
    
    System.out.print("Enter the Radius -->");
    radius = input.nextFloat();
    
    area = 3.141 * radius * radius;
    perimeter = 2 * radius * radius;
    
    System.out.printf("Area = %.2f and Perimeter = %.2f", area, perimeter);
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *