Write a Java program that calculates area and perimeter of square in a function. Name Calculate Area and Calculate Perimeter respectively. The values that will be passed to the functions will be first input by user.
Source code
import java.util.Scanner;
public class Main
{
static double calculatesArea(double s){
return (s*s);
}
static double CalculatePerimeter(double s){
return (4*s);
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double side;
System.out.println("Enter the length of the sides of the square: ");
side=in.nextDouble();
System.out.println("Area of the square: "+calculatesArea(side));
System.out.println("Perimeter of the square: "+CalculatePerimeter(side));
}
}
Output
Comments
Leave a comment