Datatypes solved

In Java, different data types have different sizes and thus, can hold different ranges of numeric values. This program takes a number as input and checks which data types (byte, short, int, long) can accommodate this number.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Datatypes solved HackerRank solution using CodeRankGPT:

The program uses a Scanner to read the input number. It then uses conditional statements to check if the number fits within the range of each data type. If the number fits, it prints the corresponding data type. If an exception occurs (i.e., the number is too large for a long), it prints that the number can't be fitted anywhere.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.introduction;

import java.util.Scanner;

/**
 *
 */
public class JavaDatatypes {
	public static void main(String[] argh) {

		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();

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

			try {
				long x = sc.nextLong();
				System.out.println(x + " can be fitted in:");
				if (x >= -128 && x <= 127)
					System.out.println("* byte");
				if (x >= -(Math.pow(2, 16 - 1)) && x <= (Math.pow(2, 16 - 1) - 1))
					System.out.println("* short");
				if (x >= -(Math.pow(2, 32 - 1)) && x <= (Math.pow(2, 32 - 1) - 1))
					System.out.println("* int");
				if (x >= -(Math.pow(2, 64 - 1)) && x <= (Math.pow(2, 64 - 1) - 1))
					System.out.println("* long");

			} catch (Exception e) {
				System.out.println(sc.next() + " can't be fitted anywhere.");
			}

		}
		sc.close();
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform