0% found this document useful (0 votes)
22 views9 pages

Java If Else Statement Code:: Import Public Class Public Static Void New Int

The document contains various Java code snippets demonstrating the use of if-else statements, input/output formatting, loops, and data types. Each code example includes the expected output and the actual output, highlighting discrepancies in formatting or logic. The examples cover topics such as checking for 'weird' numbers, formatting output, generating multiples, and handling large integers with BigInteger.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views9 pages

Java If Else Statement Code:: Import Public Class Public Static Void New Int

The document contains various Java code snippets demonstrating the use of if-else statements, input/output formatting, loops, and data types. Each code example includes the expected output and the actual output, highlighting discrepancies in formatting or logic. The examples cover topics such as checking for 'weird' numbers, formatting output, generating multiples, and handling large integers with BigInteger.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVA IF ELSE STATEMENT CODE:

import java.util.Scanner;

public class WeirdNumber {

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");
}
}
}
}

OUTPUT:

Input (stdin)
 3

Your Output (stdout)


 Weird

Expected Output

Weird

Java stdin and stdout code:

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!

Your Output (stdout)


 String: Welcome to HackerRank's Java tutorials!
 Double: 3.1415
 Int: 42

Expected Output
 String: Welcome to HackerRank's Java tutorials!
 Double: 3.1415
 Int: 42

JAVA OUTPUT FORMATTING CODE:

import java.util.Scanner;

public class OutputFormatting {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("================================");

for (int i = 0; i < 3; i++) {


String str = scanner.next();
int num = scanner.nextInt();
System.out.printf("%-15s%03d%n", str, num);
}

System.out.println("================================");
scanner.close();
}
}
OUTPUT:
Input (stdin)
 java 100
 cpp 65
 python 50

Your Output (stdout)


 ================================
 java 100
 cpp 065
 python 050
 ================================

Expected Output
 ================================
 java 100
 cpp 065
 python 050
 ================================

JAVA LOOPS:
import java.util.Scanner;

public class Multiples {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.close();

for (int i = 1; i <= 10; i++) {


System.out.println(N + " x " + i + " = " + (N * i));
}
}
}

0UTPUT:
Input (stdin)

 2

Your Output (stdout)


 2x1=2
 2x2=4
 2x3=6
 2x4=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

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

JAVA LOOPS-2 CODE:


import java.util.Scanner;

public class Series {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int q = scanner.nextInt();
for (int i = 0; i < q; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int n = scanner.nextInt();

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

Your Output (stdout)


 2 6 14 30 62 126 254 510 1022 2046
 8 14 26 50 98

Expected Output

 2 6 14 30 62 126 254 510 1022 2046

 8 14 26 50 98

JAVA DATATYPES:
import java.util.Scanner;
import java.math.BigInteger;

public class DataTypes {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int testCases = scanner.nextInt();

for (int i = 0; i < testCases; i++) {


try {
BigInteger n = scanner.nextBigInteger();

// Check for fitting in various data types


System.out.println(n + " can be fitted in:");

// 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:

You might also like