Java If-Else – HackerRank Solution

Java If-Else – HackerRank Solution

Problem :

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.


  • 1 <= n <= 100

Output Format :

Print Weird if the number is weird; otherwise, print Not Weird.

Sample Input 0 :


Sample Output 0 :


Sample Input 1 :


Sample Output 1 :

Not Weird

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.

Solution :

import java.math.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(;
        int n=sc.nextInt();            
        String ans="";

        if(n>=1 && n<=100){
                ans = "Weird";
                if(n>=2 && n<=5 ){
                   ans= "Not Weird";
                else if (n>=6 && n<=20){
                    ans = "Weird";
                else if(n>20) {
                    ans = "Not Weird";
            ans="Invalid no! Enter a number between 1 and 100.";



 53 total views,  1 views today

Post Disclaimer

the above hole problem statement is given by but the solution is generated by the SLTECHACADEMY authority if any of the query regarding this post or website fill the following contact form thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *