Java If Else Statement Code:: Import Public Class Public Static Void New Int
Java If Else Statement Code:: Import Public Class Public Static Void New Int
import java.util.Scanner;
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");
}
}
}
}
OUTPUT:
Input (stdin)
3
Expected Output
Weird
import java.util.Scanner;
public class DataTypeInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int intInput = scanner.nextInt();
double doubleInput = scanner.nextDouble();
scanner.nextLine();
String stringInput = scanner.nextLine();
scanner.close();
System.out.println("String: " + stringInput);
System.out.println("Double: " + doubleInput);
System.out.println("Int: " + intInput);
}
}
Output:
42
3.1415
Welcome to HackerRank's Java tutorials!
Expected Output
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
import java.util.Scanner;
System.out.println("================================");
scanner.close();
}
}
OUTPUT:
Input (stdin)
java 100
cpp 65
python 50
Expected Output
================================
java 100
cpp 065
python 050
================================
JAVA LOOPS:
import java.util.Scanner;
0UTPUT:
Input (stdin)
2
Expected Output
2x1=2
2x2=4
2 x 3=6
2 x 4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
int seriesElement = a;
StringBuilder series = new StringBuilder();
for (int j = 0; j < n; j++) {
seriesElement += b * Math.pow(2, j);
series.append(seriesElement).append(" ");
}
System.out.println(series.toString().trim());
}
scanner.close();
}
}
OUTPUT:
Input (stdin)
2
0 2 10
535
Expected Output
8 14 26 50 98
JAVA DATATYPES:
import java.util.Scanner;
import java.math.BigInteger;
// Checking byte
if (n.compareTo(BigInteger.valueOf(Byte.MIN_VALUE)) >= 0 &&
n.compareTo(BigInteger.valueOf(Byte.MAX_VALUE)) <= 0) {
System.out.println("* byte");
}
// Checking short
if (n.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) >= 0 &&
n.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) <= 0) {
System.out.println("* short");
}
// Checking int
if (n.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0 &&
n.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0) {
System.out.println("* int");
}
// Checking long
if (n.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0 &&
n.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) {
System.out.println("* long");
}
} catch (Exception e) {
System.out.println(scanner.next() + " can't be fitted anywhere.");
}
}
scanner.close();
}
}
OUTPUT: