{"id":18344,"date":"2022-09-06T18:12:13","date_gmt":"2022-09-06T12:42:13","guid":{"rendered":"https:\/\/copyassignment.com\/?p=18344"},"modified":"2022-11-29T18:22:27","modified_gmt":"2022-11-29T12:52:27","slug":"java-datatypes-hackerrank-solution","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/java-datatypes-hackerrank-solution\/","title":{"rendered":"Java Datatypes Hackerrank Solution"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>In Java Datatypes Hackerrank Solution, we are given an integer input, we have to determine the primitive datatypes of java that can store this integer. Java has <strong><em>4 primitive datatypes<\/em><\/strong> for storing integers. They are<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Byte: It can hold 8 bit signed integer<\/strong><\/li>\n\n\n\n<li><strong>Short: It can hold 16-bit signed integer<\/strong><\/li>\n\n\n\n<li><strong>Int: It can hold a 32-bit signed integer<\/strong><\/li>\n\n\n\n<li><strong>Long: It can hold a 64-bit signed integer<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Given an input, we have to print which of these datatypes are capable of storing it.<\/p>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>Byte has <strong><em>8 bits<\/em><\/strong> of space out of which <strong><em>1 bit<\/em><\/strong> is reserved for the sign. Hence only <strong><em>7 bits<\/em><\/strong> are available. Thus a bit can store values between <strong><em>-2^7<\/em><\/strong> and <strong><em>2^7 &#8211; 1<\/em><\/strong>. Which is equivalent to <strong><em>-128 to 127<\/em><\/strong>.<\/p>\n\n\n\n<p>Similarly, short can store values between <strong><em>-2^15<\/em><\/strong> and <strong><em>2^15-1<\/em><\/strong> i.e. <strong><em>32768 to 32787<\/em><\/strong>.<\/p>\n\n\n\n<p>Int can store values between <strong><em>-2^32<\/em><\/strong> and <strong><em>2^32-1<\/em><\/strong> i.e <strong><em>-2147483647 to 2147483648<\/em><\/strong>.<\/p>\n\n\n\n<p>And long can store values between <strong><em>-2^64<\/em><\/strong> and to <strong><em>2^64-1<\/em><\/strong>.<\/p>\n\n\n\n<p>We can use these conditions in our code to find out suitable data types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Datatypes Hackerrank Solution<\/h2>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\nimport java.io.*;\n\n\nclass Solution{\n    public static void main(String &#91;]argh)\n    {\n        Scanner sc = new Scanner(System.in);\n        \n        int t=sc.nextInt();\n\n        for(int i=0;i&lt;t;i++)\n        {\n            try\n            {\n                long x=sc.nextLong();\n                if(x &gt; Long.MAX_VALUE || x &lt; Long.MIN_VALUE){\n                    System.out.println(x+\" can't be fitted anywhere.\");\n                }\n                else{\n                    System.out.println(x+\" can be fitted in:\");\n                \n                    if(x&gt;=-128 &amp;&amp; x&lt;=127){\n                        System.out.println(\"* byte\");\n                    }\n                    if (x&gt;= -32768 &amp;&amp; x &lt;= 32767){\n                        System.out.println(\"* short\");\n                    }\n                    if(x&gt;=-2147483648 &amp;&amp; x&lt;=2147483647){\n                        System.out.println(\"* int\");\n                    }\n                    if(x&gt;=Long.MIN_VALUE &amp;&amp; x&lt;=Long.MAX_VALUE){\n                        System.out.println(\"* long\");\n                    }\n                }\n            \n            }\n            catch(Exception e)\n            {\n                System.out.println(sc.next()+\" can't be fitted anywhere.\");\n            }\n\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"has-medium-font-size wp-block-heading\">Input:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>5<\/li>\n\n\n\n<li>-150<\/li>\n\n\n\n<li>150000<\/li>\n\n\n\n<li>1500000000<\/li>\n\n\n\n<li>213333333333333333333333333333333333<\/li>\n\n\n\n<li>-100000000000000<\/li>\n<\/ul>\n\n\n\n<h3 class=\"has-medium-font-size wp-block-heading\">Output:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-150 can be fitted in:\n* short\n* int\n* long\n150000 can be fitted in:\n* int\n* long\n1500000000 can be fitted in:\n* int\n* long\n213333333333333333333333333333333333 can't be fitted anywhere.\n-100000000000000 can be fitted in:\n* long<\/code><\/pre>\n\n\n\n<h3 class=\"has-medium-font-size wp-block-heading\">HackerRank ScreenShot:<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"496\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-32-1024x496.png\" alt=\"HackerRank ScreenShot for Java Datatypes Hackerrank Solution\" class=\"wp-image-18345 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-32-1024x496.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-32-300x145.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-32-768x372.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-32-1026x497.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-32-675x327.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-32.png 1138w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/496;\" \/><\/figure>\n\n\n\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-9886351916045880\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-9886351916045880\"\n     data-ad-slot=\"7933252109\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<div style=\"text-align:center\" class=\"wp-block-atomic-blocks-ab-button ab-block-button\"><a href=\"https:\/\/copyassignment.com\/top-100-python-projects-with-source-code\/\" class=\"ab-button ab-button-shape-rounded ab-button-size-medium\" style=\"color:#ffffff;background-color:#3373dc\">Best 100+ Python Projects with source code<\/a><\/div>\n\n\n\n<p>We hope we solved your problem of Java Datatypes Hackerrank Solution.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/download-1000-projects-all-b-tech-programming-notes-job-resume-interview-guide-and-more-get-your-ultimate-programming-bundle\/\">Download 1000+ Projects, All B.Tech &#038; Programming Notes, Job, Resume &#038; Interview Guide, and More &#8211; Get Your Ultimate Programming Bundle!<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-8-solution-in-python-dictionaries-and-maps\/\">HackerRank Day 8 Solution in Python: Dictionaries and Maps<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-7-solution-in-python-arrays\/\">HackerRank Day 7 Solution in Python: Arrays<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-6-solution-in-python-lets-review\/\">HackerRank Day 6 Solution in Python: Let&#8217;s review<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-5-solution-in-python-loops\/\">HackerRank Day 5 Solution in Python: Loops<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-4-solution-in-python\/\">HackerRank Day 4 Solution in Python: Class vs Instance<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-3-solution-in-python\/\">HackerRank Day 3 Solution in Python: Intro to Conditional Statements<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-2-solution-in-python-operators\/\">HackerRank Day 2 Solution in Python: Operators<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-1-solution-in-python-data-types\/\">HackerRank Day 1 Solution in Python: Data Types<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-0-solution-in-python-hello-world\/\">HackerRank Day 0 Solution in Python: Hello World<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-29-solution-in-python-bitwise-and\/\">HackerRank Day 29 Solution in Python: Bitwise AND<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-28-solution-in-python-regex-patterns-and-intro-to-databases\/\">HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-27-solution-in-python-testing\/\">HackerRank Day 27 Solution in Python: Testing<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-26-solution-in-python-nested-logic\/\">HackerRank Day 26 Solution in Python: Nested Logic<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-25-solution-in-python-running-time-and-complexity\/\">HackerRank Day 25 Solution in Python: Running Time and Complexity<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-24-solution-in-python-more-linked-lists\/\">HackerRank Day 24 Solution in Python: More Linked Lists<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-23-solution-in-python-bst-level-order-traversal\/\">HackerRank Day 23 Solution in Python: BST Level Order Traversal<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-22-solution-in-python-binary-search-trees\/\">HackerRank Day 22 Solution in Python: Binary Search Trees<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/find-peak-element-leetcode-162\/\">Find Peak Element LeetCode 162<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-20-solution-in-python-sorting\/\">HackerRank Day 20 Solution in Python: Sorting<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-19-solution-in-python-interfaces\/\">HackerRank Day 19 Solution in Python: Interfaces<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-18-solution-in-python-queues-and-stacks\/\">HackerRank Day 18 Solution in Python: Queues and Stacks<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-17-solution-in-python-more-exceptions\/\">HackerRank Day 17 Solution in Python: More Exceptions<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-16-solution-exceptions-string-to-integer\/\">HackerRank Day 16 Solution: Exceptions &#8211; String to Integer<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/explained-allocate-minimum-number-of-pages\/\">Explained: Allocate minimum number of pages<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-15-solution-in-python-linked-list\/\">HackerRank Day 15 Solution in Python: Linked List<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/search-a-2d-matrix-leetcode-74\/\">Search a 2D matrix: leetcode 74<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/maximum-subarray-sum-kadanes-algorithm\/\">Maximum Subarray Sum: Kadane&#8217;s Algorithm<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-13-solution-in-python-abstract-classes\/\">HackerRank Day 13 Solution in Python: Abstract Classes<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-14-solution-in-python-scope\/\">HackerRank Day 14 Solution in Python: Scope<\/a><\/li>\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Introduction In Java Datatypes Hackerrank Solution, we are given an integer input, we have to determine the primitive datatypes of java that can store this&#8230;<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1306],"tags":[],"class_list":["post-18344","post","type-post","status-publish","format-standard","hentry","category-allcategorites","category-competitive-programming","wpcat-22-id","wpcat-1306-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/18344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=18344"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/18344\/revisions"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=18344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=18344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=18344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}