{"id":2088,"date":"2019-05-22T04:58:00","date_gmt":"2019-05-22T04:58:00","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2088"},"modified":"2024-08-08T20:57:18","modified_gmt":"2024-08-08T20:57:18","slug":"factorial-java-program-using-recursion","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/factorial-java-program-using-recursion\/","title":{"rendered":"Factorial Java Program using Recursion [ 2024 ]"},"content":{"rendered":"<h3>Factorial Java Program using Recursion<\/h3>\r\n<p>Write a Java program to compute the factorial of a given number n using Recursion.<\/p>\r\n<h3>IPO Chart<\/h3>\r\n<h3>Input<\/h3>\r\n<p>We will take input from the user for n<\/p>\r\n<h3>Process\u00a0<\/h3>\r\n<p>For n&gt;= 0 , we will do a recursive call using the below formula<\/p>\r\n<p><strong>fact(n) = n* fact(n-1)<\/strong><\/p>\r\n<h3>Output<\/h3>\r\n<p>factorial of n.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2100\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Math-Formula.png\" alt=\"\" width=\"1660\" height=\"848\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Math-Formula.png 1660w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Math-Formula-300x153.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Math-Formula-1024x523.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Math-Formula-768x392.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Math-Formula-1536x785.png 1536w\" sizes=\"auto, (max-width: 1660px) 100vw, 1660px\" \/><\/p>\r\n<h3>\u00a0<\/h3>\r\n<h3>Java Program<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package recursion;\r\n\r\nimport java.util.Scanner;\r\n\r\n\/**************************************************\r\n * FactorialRecursion.java\r\n * @program   \t: Factorial Using Recursion\r\n * @web        \t: www.TestingDocs.com\r\n * @author      :  \r\n * @version     : \r\n **************************************************\/\r\npublic class FactorialRecursion {\r\n  \/\/Main method\r\n  public static void main(String[] args) {\r\n    int n;\r\n    Scanner input = new Scanner(System.in);\r\n    System.out.println(\"Enter positive n:= \");\r\n    n = input.nextInt();\r\n\r\n    if(n &gt;= 0)\r\n      System.out.println(\"Factorial of  \" + n + \" is = \" + factRecurFun(n));\r\n    else\r\n      System.out.println(\"Enter valid input.\");\r\n  }\r\n\r\n  \/**************************************************\r\n   * factRecurFun() is a recursive method to compute\r\n   * factorial of the given number.\r\n   **************************************************\/\r\n  public static int factRecurFun(int n) {\r\n    \/\/ base case\r\n    if(n &lt; 1 )   \r\n      return 1;\r\n    else\r\n      \/\/Recursive call\r\n      return n*factRecurFun(n-1);\r\n  }\r\n}\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<h3>Program Output<\/h3>\r\n<p>Enter positive n:= <br \/>6<br \/>Factorial of 6 is = 720<\/p>\r\n<h3>Screenshot<\/h3>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2089\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Using-Recursion-Java-Program.png\" alt=\"\" width=\"1752\" height=\"1028\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Using-Recursion-Java-Program.png 1752w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Using-Recursion-Java-Program-300x176.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Using-Recursion-Java-Program-1024x601.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Using-Recursion-Java-Program-768x451.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Factorial-Using-Recursion-Java-Program-1536x901.png 1536w\" sizes=\"auto, (max-width: 1752px) 100vw, 1752px\" \/><\/p>\r\n<p>&nbsp;<\/p>\r\n<h2>Java Tutorials<\/h2>\r\n<p>Java Tutorial on this website:<\/p>\r\n<ul>\r\n<li><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/li>\r\n<\/ul>\r\n<p><br \/>For more information on Java, visit the official website :<\/p>\r\n<ul>\r\n<li><a href=\"https:\/\/www.oracle.com\/in\/java\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/li>\r\n<\/ul>\r\n","protected":false},"excerpt":{"rendered":"<p>Factorial Java Program using Recursion : Write a Java program to compute the factorial of a given number n using Recursion. IPO Chart<\/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-2088","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\/2088","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=2088"}],"version-history":[{"count":7,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2088\/revisions"}],"predecessor-version":[{"id":23571,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2088\/revisions\/23571"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}