Circle Perimeter Calculation

Simple Java program to calculate the perimeter of a circle using user input radius with validation and precise output formatting.

Brief Introduction

Circle Perimeter Calculation (java)
DESCRIPTION:
Simple Java program that calculates the perimeter of a circle given the radius entered by the user.

ADDITIONAL NOTES:
The program is simple and robust: it checks that the radius is positive and handles keyboard input. It uses only standard Java libraries, so it is ready to use without additional configurations.

EXPLANATIONS:
- Use of Scanner to read keyboard input in a simple and standard way.
- Check of the entered value to avoid negative or zero values, which do not make sense for a radius.
- Calculation of the perimeter using the Math.PI constant for precision.
- Formatted printing with printf to show the result with two decimal places.
- Closing the scanner to avoid warnings and free resources.

Example Code: java

import java.util.Scanner;

/**
 * Program to calculate the perimeter of a circle.
 * The user enters the radius, the program calculates and prints the perimeter.
 */
public class CalcoloPerimetroCerchio {
    public static void main(String[] args) {
        // Create scanner to read input from keyboard
        Scanner scanner = new Scanner(System.in);

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

        // Read the radius as double
        double raggio = scanner.nextDouble();

        // Check that the radius is positive
        if (raggio <= 0) {
            System.out.println("Error: the radius must be a positive number.");
            scanner.close();
            return; // end the program
        }

        // Calculate the perimeter: 2 * pi * radius
        double perimetro = 2 * Math.PI * raggio;

        // Print the result with 2 decimals
        System.out.printf("The perimeter of the circle with radius %.2f is: %.2f\n", raggio, perimetro);

        // Close the scanner
        scanner.close();
    }
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Java JDK (version 8 or higher)

IDE or text editor for Java (e.g., IntelliJ IDEA, Eclipse, VSCode)

Dependencies: none external, only standard Java libraries

Keyboard input via Scanner (java.util.Scanner)

Output on standard console