## Write program to calculate the area of different geometric shapes such as squares, rectangles, circles, and triangles.

# Program in C

C

```
#include <stdio.h>
// Function to calculate the area of a square
float squareArea(float sideLength) {
return sideLength * sideLength;
}
// Function to calculate the area of a rectangle
float rectangleArea(float length, float width) {
return length * width;
}
// Function to calculate the area of a circle
float circleArea(float radius) {
return 3.14159 * radius * radius;
}
// Function to calculate the area of a triangle
float triangleArea(float base, float height) {
return 0.5 * base * height;
}
int main() {
float side, length, width, radius, base, height;
// Get input for square
printf("Enter the length of a side of the square: ");
scanf("%f", &side);
printf("Area of the square: %.2f\n", squareArea(side));
// Get input for rectangle
printf("Enter the length and width of the rectangle: ");
scanf("%f %f", &length, &width);
printf("Area of the rectangle: %.2f\n", rectangleArea(length, width));
// Get input for circle
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
printf("Area of the circle: %.2f\n", circleArea(radius));
// Get input for triangle
printf("Enter the base and height of the triangle: ");
scanf("%f %f", &base, &height);
printf("Area of the triangle: %.2f\n", triangleArea(base, height));
return 0;
}
```

## Explanation:

- The squareArea() function takes the length of a side as a parameter and returns the area of the square by multiplying the side length by itself.
- The rectangleArea() function takes the length and width of a rectangle as parameters and returns the area of the rectangle by multiplying the length and width.
- The circleArea() function takes the radius of a circle as a parameter and returns the area of the circle by multiplying the square of the radius by the value of π (pi). In this code, an approximation of π is used with the value 3.14159.
- The triangleArea() function takes the base and height of a triangle as parameters and returns the area of the triangle by multiplying half the base by the height.
- The main() function is where the program starts executing. It prompts the user to input the necessary values for each shape and then calls the corresponding area functions, passing the input values as arguments. The calculated areas are then displayed on the screen with two decimal places using the printf() function.

C Output

```
Enter the length of a side of the square: 10
Area of the square: 100.00
Enter the length and width of the rectangle: 5
10
Area of the rectangle: 50.00
Enter the radius of the circle: 5
Area of the circle: 78.54
Enter the base and height of the triangle: 5
4
Area of the triangle: 10.00
```

# Program in Java

Java

```
import java.util.Scanner;
public class CalculateArea {
// Function to calculate the area of a square
static float squareArea(float sideLength) {
return sideLength * sideLength;
}
// Function to calculate the area of a rectangle
static float rectangleArea(float length, float width) {
return length * width;
}
// Function to calculate the area of a circle
static float circleArea(float radius) {
return (float) (3.14159 * radius * radius);
}
// Function to calculate the area of a triangle
static float triangleArea(float base, float height) {
return 0.5f * base * height;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
float side, length, width, radius, base, height;
// Get input for square
System.out.print("Enter the length of a side of the square: ");
side = scanner.nextFloat();
System.out.printf("Area of the square: %.2f\n", squareArea(side));
// Get input for rectangle
System.out.print("Enter the length and width of the rectangle: ");
length = scanner.nextFloat();
width = scanner.nextFloat();
System.out.printf("Area of the rectangle: %.2f\n", rectangleArea(length, width));
// Get input for circle
System.out.print("Enter the radius of the circle: ");
radius = scanner.nextFloat();
System.out.printf("Area of the circle: %.2f\n", circleArea(radius));
// Get input for triangle
System.out.print("Enter the base and height of the triangle: ");
base = scanner.nextFloat();
height = scanner.nextFloat();
System.out.printf("Area of the triangle: %.2f\n", triangleArea(base, height));
scanner.close();
}
}
```

## Explanation:

- The code defines a Java class named Main.
- The squareArea, rectangleArea, circleArea, and triangleArea functions calculate the areas of the respective shapes based on the provided dimensions.
- In the main function:
- A Scanner object named scanner is created to read input from the user.
- Variables side, length, width, radius, base, and height are declared to store the user input.
- The user is prompted to enter the dimensions for each shape using System.out.print.
- The user input is obtained using the scanner.nextFloat() method.
- The corresponding area functions are called with the user input as arguments, and the results are displayed using System.out.printf.
- Finally, the scanner is closed to release system resources.

Java Output

```
Enter the length of a side of the square: 4
Area of the square: 16.00
Enter the length and width of the rectangle: 10
4
Area of the rectangle: 40.00
Enter the radius of the circle: 4
Area of the circle: 50.27
Enter the base and height of the triangle: 4
8
Area of the triangle: 16.00
```