## Saturday, September 30, 2017

### if - else if-else + & Operator

Challenge: A compass needle points a given number of degrees away from North, measured clockwise. Write a program that reads the angle and prints out the nearest compass direction; one of N, NE, E, SE, S, SW, W, NW. In the case of a tie, prefer the nearest principal direction (N, E, S, or W).

Solution:

import java.util.Scanner;

public class lab4_4
{
public static void main(String []args)
{
//double a=2,b=1;
Scanner in= new Scanner(System.in);

double angle;
System.out.println("Please enter an angle");
angle=in.nextDouble();
if (angle>0 && angle<=22.5)
{
System.out.println("You are heading NORTH");
}
else if(angle>22.5 && angle<67.5)
{
System.out.println("You are heading NORTH-EAST ");
}
else if(angle>=67.5 && angle<=112.5)
{
System.out.println("You are heading EAST ");
}
else if(angle>112.5 && angle<157.5)
{
System.out.println("You are heading SOUTH-EAST ");
}

else if(angle>=157.5 && angle<=202.5)
{
System.out.println("You are heading SOUTH");
}
else if(angle>202.5 && angle<247.5)
{
System.out.println("You are heading SOUTH-WEST");
}
else if(angle>=247.5 && angle<=292.5)
{
System.out.println("You are heading WEST");
}
else if(angle>292.5 && angle<337.5)
{
System.out.println("You are heading NORTH-WEST");
}
else if(angle>=337.5 && angle<=360.5)
{
System.out.println("You are heading NORTH");
}
else
{
System.out.println("Are you lost?");
}
}
}
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}