Don't Wait,Do it yourself.

Latest courses

Hackerank Day 3: Intro to Conditional Statements solution in java


Day 3: Intro to Conditional Statements

Hackerank Day 3: Intro to Conditional Statements solution in java


Algorithm

input_number=N
Step:-1   First check the input number is even or odd.
Step:-2   if(even(N) AND(N==2 OR 4)) then print("Not Weird");
Step:-3   else if(even(N) AND(N>=6 OR N<=20)) then  print("Weird");
Step:-3   else if(even(N) AND (N>20)) then print("Not Weird");
Step:-3   else print("Weird");

Explanation

At first, Check the number is even or odd by using modulo operator(%) and other techniques.If the input_number(N) is odd then directly print out "Weird" and if the input_number is even then there are three condition which are check one by one with the conditional statement.
First condition:- if it is  2 or 4 then print out "Not Weird".
Second Condition:- if it is within the range inclusion 6 to 20 (6<=N<=20) then print out "Weird".
Third Condition:- if it greater than 20 (N>20) then print out "Not Weird".

Examples

Program

 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 && (N==2 || N==4))
{
System.out.println("Not Weird");
}

else if(N%2==0 && (N>=6 && N<=20))
{
System.out.println("Weird");
}
else if(N%2==0 && N>20)
{
System.out.println("Not Weird");
}
else
{
System.out.println("Weird");
}


scanner.close();
}
}






No comments:

Post a Comment

Please do not enter any spam link in the comment Box