thestudentnetwork

Showing posts with label hackerrank. Show all posts
Showing posts with label hackerrank. Show all posts

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:

Hackerrank solutions: Java Stdin and Stdout



Task: In this challenge, we must read 3 integers as inputs and then print them to stdout. Each integer should be printed on a new line.

Sample Input: 

43
54
123

Sample Output:

43
54
123

Quick Bits:

Stdin: Standard Input 
Stdout: Standard Output
Imports: java.util.*; (or)  java.util.Scanner;
Using Scanner: Scanner scan = new Scanner(System.in);

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:

Hackerrank solutions: Welcome to Java!



Input: There is no input to be given in the program below.

Output: We must print the following to output lines

1. Print Hello, World. on the first line.
2. Print Hello, Java. on the second line.

Note 1: In order to print something as an output in java we use  System.out.println("Type text to be printed here...").

Note 2: println is used to print text on a new line.

Solution: 


  
Share:

Definition List

Contact

Pages

Support