1. Program to calculate the area of rectangle
import java.util.*;
public class JavaAreaAndPerimeterOfRectangle {
public static void main(String args[]) {
int length, width, area, perimeter;
Scanner in = new Scanner(System.in);
System.out.println("Enter length of Rectangle");
length = in.nextInt();
System.out.println("Enter width of Rectangle");
width = in.nextInt();
// Area of rectangle = length X width
area = length * width;
// Perimeter of rectangle = 2 X (length X width)
perimeter = 2 * (length + width);
System.out.println("Area of Rectangle : " + area);
System.out.println("Rectangle of Rectangle : " + perimeter);
}
}
Output :-
Enter length of Rectangle:
30
Enter width of Rectangle:
20
Area of Rectangle : 600
Rectangle of Rectangle : 100
2. Program to calculate the parameters of Circle
import java.util.Scanner;
public class JavaAreaCircumferenceCircle {
static Scanner sc = new Scanner(System.in);
public static void main(String args[]) {
System.out.print("Enter the radius: ");
double radius = sc.nextDouble();
// Area = PI*radius*radius
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
// Circumference = 2*PI*radius
double circumference = Math.PI * 2 * radius;
System.out.println("The circumference of the circle is:" + circumference);
}
}
Output :-
Enter the radius: 25
The area of circle is: 1963.4954084936207
The circumference of the circle is:157.07963267948966
3. Program to calculate the area of Triangle
import java.util.Scanner;
public class JavaAreaOfTriangle {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the width of the Triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the Triangle:");
double height = scanner.nextDouble();
double area = (base * height) / 2;
System.out.println("Area of Triangle is: " + area);
}
}
Output :-
Enter the width of the Triangle:
25
Enter the height of the Triangle:
25
Area of Triangle is: 312.5
4. Program to calculate the area of Square
import java.util.Scanner;
public class JavaAreaOfSquare {
public static void main(String[] args) {
System.out.println("Enter Side of Square:");
// Capture the user's input
Scanner scanner = new Scanner(System.in);
// Storing the captured value in a variable
double side = scanner.nextDouble();
// Area of Square = side*side
double area = side * side;
System.out.println("Area of Square is: " + area);
}
}
Output :-
Enter Side of Square:
25
Area of Square is: 625.0
5. Program to find the area of Parallelogram
//Java Program to Find the Area of a Parallelogram
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class AreaOfAParallelogram {
// Function to calculate the area of a parallelogram
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double base,height;
System.out.println("Enter the base and the height of the parallelogram");
try{
base=Double.parseDouble(br.readLine());
height=Double.parseDouble(br.readLine());
}catch (Exception e){
System.out.println("An error occurred");
return;
}
if(base<=0 || height<=0){
System.out.println("Wrong Input");
return;
}
System.out.println("Area = " + base*height );
}
}
Output :-
Enter the base and the height of the parallelogram
25
30
Area = 750.0
6. Program to find the Surface area and Volume of Cone
//Java Program to Find the Surface Area and Volume of a Cone
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Cone {
// Function to calculate and print the surface area and volume of a cone
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double radius,height;
System.out.println("Enter the radius and
height of the right circular cone");
try{
radius=Double.parseDouble(br.readLine());
height=Double.parseDouble(br.readLine());
}catch (Exception e){
System.out.println("An error occurred");
return;
}
if(radius<=0 || height<=0){
System.out.println("Wrong Input");
return;
}
double slantheight = Math.sqrt(Math.pow(radius,2) + Math.pow(height,2));
System.out.println("Volume = " + (Math.PI*Math.pow(radius,2)*height/3));
System.out.println("Surface area = " + ((Math.PI*radius*slantheight)
+ (Math.PI*radius*radius)));
}
}
Output :-
Enter the radius and height of the right circular cone :25
30
Volume = 19634.954084936206
Surface area = 5030.573284059921
7. Program to find the Area of Rhombus
//Java Program to Find the Area of a Rhombus
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class AreaOfARhombus {
// Function to calculate the area of a rhombus
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double l1,l2;
System.out.println("Enter the length of the diagonals of the rhombus");
try{
l1=Double.parseDouble(br.readLine());
l2=Double.parseDouble(br.readLine());
}catch (Exception e){
System.out.println("An error occurred");
return;
}
if(l1<=0 || l2<=0){
System.out.println("Wrong input");
return;
}
System.out.println("Area = " + (l1*l2)/2 );
}
}
Output :-
Enter the length of the diagonals of the rhombus:
25
30
Area = 375.0
0 Comments