 <p>You can simply put the conditions with the range of data types and check whether the input number falls in which data type.</p> <pre><code> class FindDataType { public static void main(String[] argh) { Scanner sc = new Scanner(System.in); //no. of input values int t = sc.nextInt(); for (int i = 0; i &lt; t; i++) { try { //Take input as long data type long x = sc.nextLong(); System.out.println(x + " can be fitted in:"); //Putting conditions to check the data type if (x &gt;= -128 &amp;&amp; x &lt;= 127) { System.out.println("* byte"); System.out.println("* short"); System.out.println("* int"); System.out.println("* long"); } else if (x &gt;= -32768 &amp;&amp; x &lt;= 32767) { System.out.println("* short"); System.out.println("* int"); System.out.println("* long"); } else if (x &gt;= -2147483648 &amp;&amp; x &lt;= 2147483647) { System.out.println("* int"); System.out.println("* long"); } else if (x &gt;= -9223372036854775808l &amp;&amp; x &lt;= 9223372036854775807l) { System.out.println("* long"); } } catch (Exception e) { //Printing exception if no data type matches. System.out.println(sc.next() + " can't be fitted anywhere."); } } sc.close(); }} </code></pre>