-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestDistance.java
More file actions
42 lines (42 loc) · 1016 Bytes
/
shortestDistance.java
File metadata and controls
42 lines (42 loc) · 1016 Bytes
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
35
36
37
38
39
40
41
42
import java.util.Scanner;
public class shortestDistance {
public static void Distance(String str)
{
int x=0;
int y=0;
for(int i =0 ; i<str.length() ; i++)
{
if(str.charAt(i)== 'E')
{
x=x+1;
}
else if(str.charAt(i) == 'W')
{
x=x-1;
}
else if( str.charAt(i) == 'N')
{
y +=1;
}
else if (str.charAt(i) == 'S')
{
y -=1;
}
else
{
System.out.println("Not Possible");
return;
}
}
double distance =0;
distance = Math.sqrt((x*x + y*y));
System.out.println(distance);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
str.toUpperCase();
Distance(str);
sc.close();
}
}