{"id":19102,"date":"2017-03-04T10:05:49","date_gmt":"2017-03-04T10:05:49","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=19102"},"modified":"2021-03-21T07:56:12","modified_gmt":"2021-03-21T07:56:12","slug":"cube-of-a-number-java-program","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/cube-of-a-number-java-program\/","title":{"rendered":"Cube of a Number Java Program"},"content":{"rendered":"<h3>Overview<\/h3>\n<p>In this tutorial, we will develop a Java Program to compute Cube of a Number. The program should prompt the user to input the number. The simplest way to compute the cube of a number is:<\/p>\n<p>output= n*n*n;<\/p>\n<h3>Math.pow()<\/h3>\n<p>However, in Java we can leverage the Math class helper static methods. We cannot instantiate the Math class, nut we can invoke static methods of this class.For the sake of this example, we can use the pow() method.<\/p>\n<p>This method returns the result of the value of the first parameter raised to the power of second parameter. To compute cube the second argument for the method should be three.<\/p>\n<p><strong>Example usage: Math.pow(n,3)<\/strong><\/p>\n<h3>Things we need:<\/h3>\n<p>Machine with JDK Installed on it: Windows, Linux, Mac etc<\/p>\n<p>IDE like Eclipse, IntelliJ, NetBeans IDE etc.<\/p>\n<h3>Java Program<\/h3>\n<pre>\/**\r\n * Sample Java Program to compute Cube of Number\r\n *\/\r\nimport java.util.Scanner;\r\n\r\n\/**\r\n * @author testingdocs.com\r\n *\/\r\npublic class CubeOfNumber {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner keyBoard = new Scanner(System.in);\r\n\t\tSystem.out.print(\"Enter the number(n):= \");\r\n\/\/prompt user for number\r\n\t\tint n= keyBoard.nextInt();\r\n\t\tdouble output = Math.pow(n,3);\/\/compute\r\n\t\tSystem.out.println(\"Cube of the number \" + n \r\n+ \" is := \" + output);\/\/display\r\n\t}\r\n}\r\n\r\n<\/pre>\n<h3>Sample Output<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-19110 size-full\" title=\"Cube of Number Java Program\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Cube-of-Number-Java-Program.png\" alt=\"Cube of Number Java Program\" width=\"1696\" height=\"834\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Cube-of-Number-Java-Program.png 1696w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Cube-of-Number-Java-Program-300x148.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Cube-of-Number-Java-Program-1024x504.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Cube-of-Number-Java-Program-768x378.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Cube-of-Number-Java-Program-1536x755.png 1536w\" sizes=\"auto, (max-width: 1696px) 100vw, 1696px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>This program uses Scanner class to take input from the user. To import Scanner class, hover on the class name and select the java.util Scanner.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-19150\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-to-you-class.png\" alt=\"Import Scanner to your class\" width=\"1376\" height=\"744\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-to-you-class.png 1376w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-to-you-class-300x162.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-to-you-class-1024x554.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Import-Scanner-to-you-class-768x415.png 768w\" sizes=\"auto, (max-width: 1376px) 100vw, 1376px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong><a href=\"https:\/\/testingdocs.com\/java-reading-from-standard-input\/\" rel=\"noopener\">https:\/\/testingdocs.com\/java-reading-from-standard-input\/<\/a><\/strong><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-19102\" data-postid=\"19102\" class=\"themify_builder_content themify_builder_content-19102 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview In this tutorial, we will develop a Java Program to compute Cube of a Number. The program should prompt the user to input the number. The simplest way to compute the cube of a number is: output= n*n*n; Math.pow() However, in Java we can leverage the Math class helper static methods. We cannot instantiate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-19102","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=19102"}],"version-history":[{"count":16,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19102\/revisions"}],"predecessor-version":[{"id":19785,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19102\/revisions\/19785"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=19102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=19102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=19102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}