Skip to main content

Command Palette

Search for a command to run...

Java Programming : Diameter and circumference of the circle

Published
2 min readView as Markdown
Java Programming : Diameter and circumference of the circle

Example

Input 
enter the radius of the circle 5
Output 
The diameter of the circle is : 10 
The circumference of the circle is: 31.4

Required to know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of functions like System.out.print(), and Use of Data types.

Formula

diameter of the circle = radius of the circle * 2 
circumference of the circle = 2 * 3.14 * radius of the circle

Steps

  • Take the radius as input from the circle.

  • Use the formula to find the Diameter and circumference of the circle.

  • Print the value of the Diameter and circumference of the circle.

Program

import java.util.Scanner;

public class Circle {

    /**
     * This class calculates the diameter and circumference of a circle.
     */

    public static void main(String[] args) {

        // Create a Scanner object to read user input.
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter the radius of the circle.
        System.out.print("Enter the radius of the circle: ");
        double radius = sc.nextDouble();

        // Calculate the diameter of the circle.
        double diameter = 2 * radius;

        // Calculate the circumference of the circle.
        double circumference = 2 * Math.PI * radius;

        // Print the diameter and circumference of the circle.
        System.out.println("The diameter of the circle is " + diameter);
        System.out.println("The circumference of the circle is " + circumference);
    }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.