In this tutorial, we will write below Java programs:
- Java Program to Calculate Area of Square
- Java Program to Calculate Area of Rectangle
- Java program to calculate the area of Triangle
1. Java Program to Calculate Area of Square
package com.javaguides.java.tutorial;
import java.util.Scanner;
/**
* Java Program to Calculate Area of Square
*
* @author https://www.javaguides.net/
*
*/
public class JavaProgram {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter Side of Square:");
// 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:
10
Area of Square is: 100.0
2. Java Program to Calculate Area of Rectangle
package com.javaguides.java.tutorial;
import java.util.Scanner;
/**
* Java Program to Calculate Area of Rectangle
*
* @author https://www.javaguides.net/ *
*/
public class JavaProgram {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
// Area = length*width;
double area = length * width;
System.out.println("Area of Rectangle is:" + area);
}
}
}
Output:
Enter the length of Rectangle:
10
Enter the width of Rectangle:
20
Area of Rectangle is:200.0
3. Java program to calculate the area of Triangle
package com.javaguides.java.tutorial;
import java.util.Scanner;
/**
* Java program to calculate area of Triangle
*
* @author https://www.javaguides.net/ *
*/
public class JavaProgram {
public static void main(String[] args) {
try (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();
// Area = (width*height)/2
double area = (base * height) / 2;
System.out.println("Area of Triangle is: " + area);
}
}
}
Output:
Enter the width of the Triangle:
10
Enter the height of the Triangle:
5
Area of Triangle is: 25.0
Related Java Programs
- String Programs in Java with Output
- Java Program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains only Digits
- How to Check if the String Contains only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Check if Input String is Palindrome
- Java Program to Find all Permutations of String
- How to Remove or Trim All White Spaces from a String in Java
- How to Remove Leading and Trailing White Space From a String in Java
- Java Program to Count Duplicate Characters in a String
- Remove Character from String in Java (Java 8)
- Java Program to Count Vowels and Consonants in a String (Java 8)
- 4 Ways to Find First Non-Repeated Character in String in Java
Comments
Post a Comment
Leave Comment