If Else Problem

The problem context is about a simple Java program that takes an integer input from the user and checks if it is 'Weird' or 'Not Weird'. A number is considered 'Weird' if it is odd or if it is even and in the inclusive range of 6 to 20. Otherwise, it is 'Not Weird'.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the If Else Problem HackerRank solution using CodeRankGPT:

The solution approach is straightforward. It first checks if the number is odd using the modulus operator. If it is, it sets the answer to 'Weird'. If it's not, it checks if the number is within the range of 6 to 20. If it is, it sets the answer to 'Weird'. Otherwise, it sets the answer to 'Not Weird'. Finally, it prints the answer.


package com.javaaid.hackerrank.solutions.languages.java.introduction;

import java.util.Scanner;

/**
 * 
 *
 */
public class JavaIfElse {

	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 >= 6 && n <= 20) {
				ans = "Weird";
			} else {
				ans = "Not Weird";
			}

		}
		System.out.println(ans);
		sc.close();
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform