0% found this document useful (0 votes)
61 views2 pages

HackerRank Java Questions and Answers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

HackerRank Java Questions and Answers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Java Stdin and Stdout I

Read an integer from stdin and print it to stdout.


import java.util.Scanner;

public class Solution {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
System.out.println(number);
}
}

2. Java If-Else

Given an integer, if the number is odd, print 'Weird'. If even and in the inclusive range of 2 to

5, print 'Not Weird'. If even and in the inclusive range of 6 to 20, print 'Weird'. If even and

greater than 20, print 'Not Weird'.


import java.util.Scanner;

public class Solution {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.close();

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

3. Java Stdin and Stdout II

Read an integer, a double, and a String, and print them in reverse order.
import java.util.Scanner;

public class Solution {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
double d = scanner.nextDouble();
scanner.nextLine(); // consume the newline character
String s = scanner.nextLine();

System.out.println("String: " + s);


System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy