{"id":80459,"date":"2025-05-28T16:27:38","date_gmt":"2025-05-28T10:57:38","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=80459"},"modified":"2025-09-09T16:13:28","modified_gmt":"2025-09-09T10:43:28","slug":"how-to-find-cube-of-a-number-with-javascript","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-find-cube-of-a-number-with-javascript\/","title":{"rendered":"How to Find the Cube of a Number Using JavaScript"},"content":{"rendered":"\n<p>In this post, we&#8217;ll explore a simple mathematical problem: <strong>finding the cube of a number<\/strong>. This is a beginner-friendly task, great for understanding basic input\/output and arithmetic operations in JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Problem Statement<\/strong><\/h2>\n\n\n\n<p>You are given a single positive integer N.<\/p>\n\n\n\n<p><strong>Input<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A single line containing a positive integer N.<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A single line containing the cube of N (i.e., N \u00d7 N \u00d7 N or N\u00b3).<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sample<\/strong><\/h2>\n\n\n\n<p><strong>Input<\/strong>:<\/p>\n\n\n\n<p>2<\/p>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>8<\/p>\n\n\n\n<p>Why?<br>Because: 2 \u00d7 2 \u00d7 2 = 8<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step-by-Step Explanation<\/strong><\/h2>\n\n\n\n<p>Here\u2019s how you should approach this:<\/p>\n\n\n\n<ol>\n<li><strong>Read the input number<\/strong>.<br><\/li>\n\n\n\n<li><strong>Calculate the cube<\/strong> using multiplication or the exponentiation operator (**).<br><\/li>\n\n\n\n<li><strong>Print the result<\/strong>.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JavaScript Solution (Node.js \/ Coding Platforms)<\/strong><\/h2>\n\n\n\n<p>Here&#8217;s a basic <a href=\"https:\/\/www.guvi.in\/hub\/javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a> script that does exactly that:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>const<\/strong> readline = require(&#8216;readline&#8217;);<br><br><strong>const<\/strong> rl = readline.createInterface({<br>&nbsp; &nbsp; input: process.stdin,<br>&nbsp; &nbsp; output: process.stdout<br>});<br><br>rl.on(&#8216;line&#8217;, <strong>function<\/strong>(line) {<br>&nbsp; &nbsp; <strong>const<\/strong> N = parseInt(line);&nbsp; &nbsp; &nbsp; \/\/ Convert input to an integer<br>&nbsp; &nbsp; <strong>const<\/strong> cube = N ** 3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Calculate cube<br>&nbsp; &nbsp; console.log(cube); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Output result<br>&nbsp; &nbsp; rl.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Close input stream<br>});<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How the Code Works<\/strong><\/h2>\n\n\n\n<ul>\n<li>We use <a href=\"https:\/\/www.guvi.in\/blog\/nodejs-libraries-and-packages\/\" target=\"_blank\" rel=\"noreferrer noopener\">Node.js&#8217;s<\/a> readline module to accept input.<br><\/li>\n\n\n\n<li>The user enters a number (say 2).<br><\/li>\n\n\n\n<li>parseInt(line) ensures the input is treated as a number, not a string.<br><\/li>\n\n\n\n<li>N ** 3 calculates the cube of N.<br><\/li>\n\n\n\n<li>Finally, we print the result using console.log().<br><\/li>\n<\/ul>\n\n\n\n<p>Note: The exponentiation <a href=\"https:\/\/www.guvi.in\/hub\/javascript\/comparison-operators-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">operator in JavaScript<\/a>. You could also use Math.pow(N, 3) if preferred.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h2>\n\n\n\n<p>This problem is great for practicing:<\/p>\n\n\n\n<ul>\n<li>Reading input<br><\/li>\n\n\n\n<li>Doing basic arithmetic<br><\/li>\n\n\n\n<li>Outputting results<\/li>\n<\/ul>\n\n\n\n<p><em>If you want to learn and <a href=\"https:\/\/www.guvi.in\/blog\/best-reasons-to-learn-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">become proficient in JavaScript<\/a>, the <\/em><a href=\"https:\/\/www.guvi.in\/courses\/programming\/javascript-in-100-days\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=How+to+Find+the+Cube+of+a+Number+Using+JavaScript\" target=\"_blank\" rel=\"noreferrer noopener\"><em>JavaScript Course<\/em><\/a> in 100 days<em> by HCL GUVI is the perfect place to start. It offers a structured, hands-on learning path\u2014from basics like string manipulation to building full-fledged web apps\u2014all taught by industry experts.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Finding the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cube\" target=\"_blank\" rel=\"noreferrer noopener\">cube<\/a> of a number is a simple yet effective exercise for beginners to get comfortable with basic input\/output handling and arithmetic operations in JavaScript. In this problem, we learned how to:<\/p>\n\n\n\n<ul>\n<li>Read user input using Node.js (readline module)<br><\/li>\n\n\n\n<li>Convert input strings to numbers using parseInt<br><\/li>\n\n\n\n<li>Use exponentiation (**) to calculate the cube<br><\/li>\n\n\n\n<li>Display the result with console.log<br><br><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we&#8217;ll explore a simple mathematical problem: finding the cube of a number. This is a beginner-friendly task, great for understanding basic input\/output and arithmetic operations in JavaScript. Problem Statement You are given a single positive integer N. Input: Output: Sample Input: 2 Output: 8 Why?Because: 2 \u00d7 2 \u00d7 2 = 8 [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":82984,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[429,294,907],"tags":[],"views":"2753","authorinfo":{"name":"Arun Kumar","url":"https:\/\/www.guvi.in\/blog\/author\/arun-kumar\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/05\/Find-the-Cube-of-a-Number-Using-JavaScript-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/05\/Find-the-Cube-of-a-Number-Using-JavaScript.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80459"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=80459"}],"version-history":[{"count":4,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80459\/revisions"}],"predecessor-version":[{"id":86756,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80459\/revisions\/86756"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/82984"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=80459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=80459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=80459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}