# Basic Java programming exercises

 Java Tutorial Java Exercises Data Structures

## Java program to find diameter, circumference and area of circle

Write a Java program to input radius of a circle from user and find diameter, circumference and area of the circle. How to calculate diameter, circumference and area of a circle whose radius is given by user in Java programming. Logic to find diameter, circumference and area of a circle in Java.

##### Required knowledge

Arithmetic operators, Data types, Defining constants, Basic input/output

##### Properties of circle

Diameter, circumference and area of a circle formula is given by - Where r is radius of the circle.

##### Logic to find diameter, circumference and area of circle

Below is the step by step descriptive logic to find diameter, circumference and area of a circle -

1. Input radius of circle from user. Store it in a variable say radius.

2. Apply the formulas to calculate diameter, circumference and area. Use diameter = 2 * radius, circumference = 2 * 3.14 * radius and area = 3.14 * radius * radius.

3. Print all resultant value diameter, circumference and area.

##### Program to find diameter, circumference and area of circle

```/**
* Java program to calculate diameter, circumference and area of circle
*/

import java.util.Scanner;

class Test
{
public static void main(String args[])
{
Scanner op=new Scanner(System.in);
/*
* Input radius of circle from user
*/

/*
* Calculate diameter, circumference and area
*/
circumference = 2 * 3.14 * radius;

/*
* Print all results
*/
System.out.println("Diameter of circle = "+diameter+" units ");
System.out.println("Circumference of circle = "+circumference+" units ");
System.out.println("Area of circle = "+area+" sq. units ");
}
}
```

##### Output:

```Enter radius of circle: 5
Diameter of circle = 10.0 units
Circumference of circle = 31.400000000000002 units
Area of circle = 78.5 sq. units

```

##### Program to find diameter, circumference and area of circle using PI constant
```/**
* Java program to calculate diameter, circumference and area of circle
*/
import java.util.Scanner;

class Test1
{
public static void main(String args[])
{
Scanner op=new Scanner(System.in);
/*
* Input radius of circle from user
*/
M_PI=22/7;
/*
* Calculate diameter, circumference and area of circle
*/
circumference = 2 * M_PI * radius;

/*
* Print all results
*/
System.out.println("Diameter of circle = "+diameter+" units");
System.out.println("Circumference of circle = "+circumference+" units ");
System.out.println("Area of circle = "+area+" sq. units ");
}
}

```

##### Output:

```Enter radius of circle: 20
Diameter of circle = 40.0 units
Circumference of circle = 120.0 units
Area of circle = 1200.0 sq. units

``` 