Java If-Else - Hacker Rank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank Java If-Else Problem with a very easy explanation. This is the 3rd problem of Java on HackerRank. In this article, you will get more than three approaches to solve this problem. So let's start-
HackerRank Java If-Else - Problem Statement
In this challenge, we test your knowledge of using if-else conditional statements to automate decision-making processes. An if-else statement has the following logical flow:
Source: Wikipedia
Task
Given an integer, n, perform the following conditional actions:
- If n is odd, print Weird
- If n is even and in the inclusive range of 2 to 5, print Not Weird
- If n is even and in the inclusive range of 6 to 20, print Weird
- If n is even and greater than 20, print Not Weird
Complete the stub code provided in your editor to print whether or not n is weird.
Input Format
A single line containing a positive integer, n.
Constraints
- 1 <= n <= 100
Output Format
Print Weird if the number is weird; otherwise, print Not Weird.
Sample Input 0
3 (code-box)
Sample Output 0
Weird (code-box)
Sample Input 1
24 (code-box)
Sample Output 1
Not Weird (code-box)
Explanation:
Sample Case 0: n = 3
n is odd and odd numbers are weird, so we print Weird.
Sample Case 1: n = 24
n > 20 and n is even, so it isn't weird. Thus, we print Not Weird.
Java If-Else - Hacker Rank Solution
Approach I:
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int N = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); if(N%2!=0) { 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 if(N>=20) { System.out.println("Not Weird"); } scanner.close(); } }
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) { Scanner sc=new Scanner(System.in); int n = sc.nextInt(); System.out.println((n%2==1 || (n>=6 && n<=20)) ? "Weird" : ((n>=2 && n <=5) || n > 20) ? "Not Weird" : "Weird"); } }
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) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String ans=""; if(n%2==1||n%2==0&& n>5 && n<21){ ans = "Weird"; } else{ if(n%2==0&& n>1 && n<6||n>20){ ans = "Not Weird"; } } System.out.println(ans); } }
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) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String ans=""; if(n%2==1){ ans = "Weird"; } else if (n>=2 && n <= 5){ ans = "Not Weird"; } else if (n>=6 && n <= 20) { ans = "Weird"; } else { ans = "Not Weird"; } System.out.println(ans); } }
Disclaimer: The above Problem ( Java If-Else ) is generated by Hackerrank but the Solution is Provided by Code Solution. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.
I hope you have understood the solution to this problem. All these four solutions will pass all the test cases. Now visit Java If-Else HackerRank Problem and try to solve it again.
All the Best!


