JAVA
Write code which takes two inputs from the user, a number of sides followed by a side length, then creates a regular polygon with that number of sides and side length.
Sample run:
Type the number of sides:
8
Type a side length:
7.5
regular octagon with side length 7.5
Hint: Make sure you use the right data types when taking user input.
import java.util.Scanner;
public class CreatePolygon {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type the number of sides:");
int sides = sc.nextInt();
System.out.println("Type a side length:");
double length = sc.nextDouble();
switch (sides) {
case 5:System.out.println("regular octagon with side length "+length);break;
case 6:System.out.println("regular hexagon with side length "+length);break;
case 7:System.out.println("regular heptagon with side length "+length);break;
case 8:System.out.println("regular octagon with side length "+length);break;
case 9:System.out.println("regular nonagon with side length "+length);break;
case 10:System.out.println("regular decagon with side length "+length);break;
case 12:System.out.println("regular dedecagon with side length "+length);break;
}
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
Get Answers For Free
Most questions answered within 1 hours.