-
Notifications
You must be signed in to change notification settings - Fork 1
/
36_AreaOfSquare.java
34 lines (26 loc) · 1.05 KB
/
36_AreaOfSquare.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
public class AreaOfSquare {
/*
* Method to calculate the area of a square given its side length
* Time Complexity: O(1) as it performs a constant number of operations
* Space Complexity: O(1) as it uses a constant amount of extra space
*/
public static float areaOfSquare(float a) {
// Return the area of the square by squaring the side length
return (a * a);
}
public static void main(String[] args) {
// Create a Scanner object for input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter the side length of the square
System.out.print("Enter side of square: ");
// Read the side length as a float
float a = sc.nextFloat();
// Compute the area of the square using the areaOfSquare method
float ans = areaOfSquare(a);
// Print the calculated area
System.out.println("Area = " + ans);
// Close the scanner
sc.close();
}
}