{"id":282,"date":"2020-12-17T20:59:00","date_gmt":"2020-12-17T20:59:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=282"},"modified":"2024-02-09T14:48:23","modified_gmt":"2024-02-09T19:48:23","slug":"java-recursion-example","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/java-recursion-example\/","title":{"rendered":"Java recursion example"},"content":{"rendered":"\n<p>Recursion is when a function calls itself repeatedly. The classical example of recursion the factorial function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5! = 5 x 4 x 3 x 2 x 1\nn! = n * (n-1) * (n-2) * (n-3) * (n-4) * (n-5)\nn! = n * (n-1)!\n5! = 5 * 4!\n5! = 5 * 4 * 3!\n...<\/code><\/pre>\n\n\n\n<p>To create a recursive definition of this function, we need to define a recursive case and a base case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>recursive case: n! = n * (n - 1)! =&gt; fact(n) = n x fact(n -1) when n &gt;= 1\nbase case: if n = 0, fact(n) = 1<\/code><\/pre>\n\n\n\n<p>The program<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Factorial {\n   public static void main(String&#91;] args) {\n      int n = 12;\n      System.out.println(n + \"! = \" + fact(n));\n   }\n\n   public static int fact(int n) {\n      if (n == 0) {\n         return 1;\n      } else {\n         return n * fact(n-1);\n      }\n   }\n}<\/code><\/pre>\n\n\n\n<p><strong>output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>12! = 479001600<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recursion is when a function calls itself repeatedly. The classical example of recursion the factorial function. To create a recursive definition of this function, we need to define a recursive case and a base case. The program output<\/p>\n","protected":false},"author":1,"featured_media":389,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38,76],"class_list":["post-282","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-programming"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=282"}],"version-history":[{"count":2,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/282\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/282\/revisions\/390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/389"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}