thestudentnetwork

Thursday, 15 September 2016

HACKERRANK SOLUTIONS: JAVA IF-ELSE


Task:
In this challenge, we have to test our knowledge on if-else conditional statement and perform the following conditional actions
i. If n is odd, print Weird.
ii. If n is even and in inclusive range of 2 to 5, print Not Weird.
iii. If n is even and in inclusive range of 6 to 20, print Weird.
iv. If n is even and greater than 20, print Not Weird.

Sample Input:

3

Sample Output;

Weird

Quick bits:

  • Stdin: Standard Input
  • Stdout: Standard Output
  • Imports: java.util.*; (or) java.util.Scanner;
  • Conditional statement: If-else
  • Syntax: If(condition)
                  {
                      ...........
                      ...........
                  }
             else
                  {
                      ...........
                      ...........
                  }

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) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();            
String ans="";
if(n%2==1){
ans = "Weird";
}
else{
         

                if(n%2==0)
                {
                if(n>2 && n<5)
                    {
                    ans="Not Weird";
                }
                if(n>6 && n<=20)
                    {
                    ans= "Weird";
                }
                if(n>20)
                    {
                    ans="Not Weird";
                }
            }
              
                
            }
                
            
            System.out.println(ans);
            
        }
    }
Share:

2 comments:

Definition List

Contact

Pages

Support