- Problem Statement
Harry is developing a graphics application that allows users to calculate the area of different shapes. Users can input the shape type (either SQUARE or TRIANGLE) and relevant dimensions.
The task is to write a program to accommodate the SQUARE and TRIANGLE shapes in the enum Shape to find the area of squares and triangles.
Formula :
Area of square = sideLength * sideLength
Area of triangle = 0.5 * base * height
Input format :
The first line of input consists of a string, representing the shape (SQUARE or TRIANGLE).
If the shape is ‘SQUARE’, the second line consists of double value a, representing the side of the square.
If the shape is ‘TRIANGLE’, the second line consists of two double values b and h, representing the base and height of the triangle, respectively.
Output format :
If the shape type is valid, print the area of the shape. If the shape type is invalid, print “Invalid Input”.
Refer to the sample output for formatting specifications.
Code constraints :
The input is case-insensitive.
0.1 ≤ a , b , h ≤ 100.0
Sample test cases :
Input 1 :
SqUARe 5.5
Output 1 :
30.25
Input 2 :
CIRCLE
Output 2 :
Invalid Input
Input 3 :
TRIANGLE 4.0 6.0
Output 3 :
12.0
// You are using Java
import java.util.Scanner;
class main{
enum Shape{
SQUARE,
TRIANGLE
}
public static void main(String args[]){
Scanner obj = new Scanner(System.in);
String shape = obj.nextLine().trim().toUpperCase();
try{
Shape Sname = Shape.valueOf(shape);
if(Sname == Shape.SQUARE){
double a = obj.nextDouble();
double result = a * a;
System.out.println(result);
}else if(Sname == Shape.TRIANGLE){
double b = obj.nextDouble();
double h = obj.nextDouble();
double area = (0.5)*b*h;
System.out.println(area);
}
}catch (IllegalArgumentException e){
System.out.println("Invalid Input");
}
}
}
Source: NeoColab(Only for providing solution to the neocolab problems)
2. Problem Statement
Alex is fascinated by mountains and valleys. He defines a peak element as an element that is strictly greater than its neighbours.
Given an array of integers, help Alex find a peak element and its index.
Input format :
The first line of input consists of an integer N, representing the size of the array.
The second line consists of N space-separated integers, representing the elements of the array.
Output format :
The output prints “Peak Element: X at index Y” where X and Y are integers representing the peak element and index of the peak element (index starts from 0)
Refer to the sample output for formatting specifications.
Code constraints :
1 ≤ N ≤ 10
Sample test cases :
Input 1 :
5 1 3 20 4 1
Output 1 :
Peak Element: 20 at index 2
Input 2 :
4 7 4 3 8
Output 2 :
Peak Element: 8 at index 3
import java.util.Scanner;
class Main {
// You are using Java
public static void main(String[] args) {
//Type your code here
Scanner obj = new Scanner(System.in);
int n = obj.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++){
arr[i] = obj.nextInt();
}
int max = Integer.MIN_VALUE;
int idx= -1;
for(int i = 0; i < n; i++){
if(arr[i] > max){
max = arr[i];
idx = i;
}
}
System.out.println("Peak Element: "+ max +" at index "+idx);
}
}
Source: NeoColab(Only for providing solution to the neocolab problems)