Using "if-else" you can perform decision making in Java. See the flowchart below (taken from wikipedia):
This problem will test your knowledge on "if-else" statements.
Given an integerN as input, check the following:
This problem will test your knowledge on "if-else" statements.
Given an integer
- If
N is odd, print "Weird". - If
N is even and, in between the range of 2 and 5(inclusive), print "Not Weird". - If
N is even and, in between the range of 6 and 20(inclusive), print "Weird". - If
N is even andN>20 , print "Not Weird".
Input Format
There is a single line of input: integer N .
Constraints
Output Format
Print "Weird" if the number is weird. Otherwise, print "Not Weird". Do not print the quotation marks.
Sample Input 1
Sample Input 1
3
Sample Output 1Weird
Sample Input 224
Sample Output 2Not Weird
Java If-Else - Hacker Rank Solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(br.readLine().trim());
if(N<1||N>100)
throw new Exception();
if((N&1)==1)
{
System.out.println("Weird");
}
else
{
if(N>=2&&N<=5)
{
System.out.println("Not Weird");
}
else if(N>=6&&N<=20)
{
System.out.println("Weird");
}
else
{
System.out.println("Not Weird");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Java If-Else - Hacker Rank Solution
Very informative. Java Training Course In Amravati
ReplyDelete