{"id":2029,"date":"2017-05-21T05:14:28","date_gmt":"2017-05-21T05:14:28","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2029"},"modified":"2021-03-21T07:32:36","modified_gmt":"2021-03-21T07:32:36","slug":"java-program-to-compute-the-factorial-of-given-number","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-program-to-compute-the-factorial-of-given-number\/","title":{"rendered":"Java program to compute the factorial of given number"},"content":{"rendered":"<h3>Program Description<\/h3>\r\n<p>Write a java program to compute the factorial of a given number.<\/p>\r\n<h3>IPO Chart<\/h3>\r\n<h3>Input<\/h3>\r\n<p>We will take input from the user for the given number. Let&#8217;s call this number n.<\/p>\r\n<h3>Process\u00a0<\/h3>\r\n<p>Compute n!<\/p>\r\n<p><strong>n! = (n)*(n-1)*(n-2)*&#8230;&#8230; * 1\u00a0<\/strong><\/p>\r\n<h3>Output<\/h3>\r\n<p>The program will output n! for the given n number.<\/p>\r\n<p>&nbsp;<\/p>\r\n<h3>Java Program<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Scanner;\r\n\r\n\/**************************************************\r\n * FactorialNumber.java\r\n * @program   \t: Factorial of n \r\n * @web        \t: www.TestingDocs.com\r\n * @version     : 1.0\r\n **************************************************\/\r\n\r\npublic class FactorialNumber { \r\n  public static void main(String args[]){\r\n    int i,fact; \r\n    Scanner keyboard= new Scanner(System.in);\r\n    System.out.println(\"Enter n : =\"); \r\n    int number= keyboard.nextInt();\/\/It is the number to calculate factorial \r\n    \/\/ positive number part\r\n    if(number&gt;= 0) {\r\n      fact=1;\r\n      for(i=1;i&lt;=number;i++){\r\n        fact=fact*i; \r\n      }\r\n    }\r\n    \/\/negative number part\r\n    else {\r\n      fact=1;\r\n      for(i=1;i&lt;=-number;i++){\r\n        fact=fact*i; \r\n      }\r\n      fact = fact*-1;\r\n    }\r\n    System.out.println(\"Factorial of \"+number+\" is: \"+fact);\r\n\r\n  } \r\n}\r\n\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n<h3>Program Output<\/h3>\r\n<p>Enter n : =<br \/>5<br \/>Factorial of 5 is: 120<\/p>\r\n<h3>Screenshot<\/h3>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2083\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-of-Number-Java-Program.png\" alt=\"\" width=\"1635\" height=\"938\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-of-Number-Java-Program.png 1635w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-of-Number-Java-Program-300x172.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-of-Number-Java-Program-1024x587.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-of-Number-Java-Program-768x441.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-of-Number-Java-Program-1536x881.png 1536w\" sizes=\"auto, (max-width: 1635px) 100vw, 1635px\" \/><\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Factorial program using Recursion:<\/strong><\/p>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/questions\/factorial-java-program-using-recursion\/\">https:\/\/www.testingdocs.com\/questions\/factorial-java-program-using-recursion\/<\/a><\/p>\r\n<p>&nbsp;<\/p>\r\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2029\" data-postid=\"2029\" class=\"themify_builder_content themify_builder_content-2029 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Program Description Write a java program to compute the factorial of a given number. IPO Chart Input We will take input from the user for the given number. Let&#8217;s call this number n. Process\u00a0 Compute n! n! = (n)*(n-1)*(n-2)*&#8230;&#8230; * 1\u00a0 Output The program will output n! for the given n number. &nbsp; Java Program [&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-2029","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\/2029","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=2029"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2029\/revisions"}],"predecessor-version":[{"id":19768,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2029\/revisions\/19768"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}