Write a Java program to calculate Area and Perimeter of a Circle?
The given example will teach you the method for preparing a program to calculate the area and perimeter of a circle.
- Name a class as "Circle" under Java I/O package
- Declare a variable radius, which is the radius of the circle.
- Use try exception to handle errors and other exceptional events.
- To input the value of radius here create a buffered class with an object as 'br'.
- This create a buffering character input stream that uses a default sized input buffer.
- The InputStreamReader here works as a translator that converts byte stream to character stream.
- Type message that "Enter radius of circle" in the System.out.println method.
- Use the parseInt() method of the Integer class, in order to convert from external numeric format to internal format.
- Create the Math class in which all the mathematical functions are defined. This Math class can be imported from the java.lang.* package.
- Write the calculation for both the cases: radius and perimeter.
- Before ending the program use the Catch mechanism that detects and catch user input errors.
- In the end compile and run the program
In this section, you will learn to calculate the area and perimeter of a circle.
Write a Java program to calculate Area of a Circle?
(Programming Model - 1)
Write a Java program to calculate Area of a Circle?
(Programming Model - 1)
import java.lang.*;
import java.io.*;
class CircleArea
{
public static void main(String args[])
{
float pi=3.14159f, area;
int r=5;
clrscr();
area = pi * r * r;
System.out.println("The area of the circle is:" + area);
}
}
import java.io.*;
class CircleArea
{
public static void main(String args[])
{
float pi=3.14159f, area;
int r=5;
clrscr();
area = pi * r * r;
System.out.println("The area of the circle is:" + area);
}
}
In this section, you will learn to calculate the area and perimeter of a circle.
Write a Java program to calculate Area and Perimeter of a Circle
(Programming Model - 2)
Write a Java program to calculate Area and Perimeter of a Circle
(Programming Model - 2)
import java.io.*;
class Circle
{
public static void main(String args[])
{
int radius;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the radius of a Circle: ");
String str = br.readLine();
radius = Integer.parseInt(str);
double area, perimeter;
area = 3.14 * radius * radius;
perimeter=2 * 3.14 * radius;
System.out.println("Area of Circle: " + area);
System.out.println("Perimeter of Circle: " + perimeter);
}
catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
class Circle
{
public static void main(String args[])
{
int radius;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the radius of a Circle: ");
String str = br.readLine();
radius = Integer.parseInt(str);
double area, perimeter;
area = 3.14 * radius * radius;
perimeter=2 * 3.14 * radius;
System.out.println("Area of Circle: " + area);
System.out.println("Perimeter of Circle: " + perimeter);
}
catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
In this section, you will learn to calculate the area and perimeter of a circle.
Write a Java program to calculate Area and Perimeter of a Circle
(Programming Model - 3)
Write a Java program to calculate Area and Perimeter of a Circle
(Programming Model - 3)
import java.io.*;
class Circle
{
public static void main(String args[]) throws IOException
{
int radius;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the radius of a Circle: ");
String str = br.readLine();
radius = Integer.parseInt(str);
double area, perimeter;
area = java.lang.Math.PI * radius * radius;
perimeter=2 * java.lang.Math.PI * radius;
System.out.println("Area of Circle: " + area);
System.out.println("Perimeter of Circle: " + perimeter);
}
catch(IOException e)
{
System.out.println("Error : "+e);
}
}
}
class Circle
{
public static void main(String args[]) throws IOException
{
int radius;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the radius of a Circle: ");
String str = br.readLine();
radius = Integer.parseInt(str);
double area, perimeter;
area = java.lang.Math.PI * radius * radius;
perimeter=2 * java.lang.Math.PI * radius;
System.out.println("Area of Circle: " + area);
System.out.println("Perimeter of Circle: " + perimeter);
}
catch(IOException e)
{
System.out.println("Error : "+e);
}
}
}
In this section, you will learn to calculate the area and perimeter of a circle.
Write a Java program to calculate Area and Perimeter of a Circle
(Programming Model - 4)
Write a Java program to calculate Area and Perimeter of a Circle
(Programming Model - 4)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Circle
{
public static void main(String args[])
{
int radius=0;
System.out.println("Enter the radius of a Circle: ");
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
radius = Integer.parseInt(br.readLine());
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid radius value" + nfe);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
radius = 5;
double area = Math.PI * radius * radius;
double perimeter=2 * Math.PI * radius;
System.out.println("Area of Circle: " + area);
System.out.println("Perimeter of Circle: " + perimeter);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class Circle
{
public static void main(String args[])
{
int radius=0;
System.out.println("Enter the radius of a Circle: ");
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
radius = Integer.parseInt(br.readLine());
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid radius value" + nfe);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
radius = 5;
double area = Math.PI * radius * radius;
double perimeter=2 * Math.PI * radius;
System.out.println("Area of Circle: " + area);
System.out.println("Perimeter of Circle: " + perimeter);
}
}
Output of the Program
Enter the radius of a Circle: 7 Area of a Circle: 78.53981633974483 Perimeter of a Circle: 31.41592653589793 |
Note: In the above program,
|