{"id":1336,"date":"2017-05-25T08:48:54","date_gmt":"2017-05-25T08:48:54","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1336"},"modified":"2024-12-14T04:38:39","modified_gmt":"2024-12-14T04:38:39","slug":"write-a-recursive-method-for-sum-of-n-integers","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-recursive-method-for-sum-of-n-integers\/","title":{"rendered":"Write a recursive method for sum of n integers?"},"content":{"rendered":"<h2>Write a recursive method for the sum of n integers?<\/h2>\n<p>Recursion is a method calling itself. Such a method is said to be a recursive method. A recursive algorithm is similar to Mathematical Induction problems. We assume that there is a solution for a base case, and we will prove the result for the general case.<\/p>\n<p>For example:<\/p>\n<p><strong>Sum(n) = 1 + 2 + 3 + \u2026. + n-1 + n<\/strong><\/p>\n<p>A base case should be stated.<\/p>\n<p><strong>Sum(1) = 1<\/strong><\/p>\n<p>While coding recursive method we need to make sure that we end it successfully. The argument passed to the recursive method should converge to the base case. Each call will invoke a reduced problem so that it will reduce to the base case.<\/p>\n<p><strong>Sum(n) = n + Sum(n-1)<\/strong><\/p>\n<h3><strong>Sample infinite loop using recursion:<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class InfiniteRecursion {\r\n\r\npublic static void main(String[] args) {\r\n infinity();\r\n System.out.println(\"I'm in main method.!\");\r\n }\r\n\r\npublic static void infinity(){\r\n infinity();\r\n }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Once the control is passed from main() to the infinity(), it will never return to the main(). infinity() method calls itself. The program falls in an infinite loop and leads to \u201cStackOverflowError\u201d. This is a common problem in recursive programs if we don\u2019t converge to the base case.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-541\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Infinite-Recursion-StackOverflowError.jpeg\" alt=\"recursive method\" width=\"1363\" height=\"739\" title=\"\"><\/p>\n<h3><\/h3>\n<h3><strong>Recursive program for Sum(n)<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.Scanner;\r\n\r\npublic class RecursiveSum {\r\n \r\n public static void main(String[] args) {\r\n Scanner in = new Scanner(System.in);\r\n System.out.println(\"Enter n\");\r\n int n = in.nextInt();\r\n System.out.println(\"Sum of n =\" + n + \" is:\" + sum(n));\r\n in.close();\r\n } \r\n \r\n public static int sum(int n){\r\n if(n==1) return 1;\r\n return n+ sum(n-1);\r\n }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-542\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Recursive-method-to-calculate-the-sum.jpeg\" alt=\"Recursive method to calculate the sum\" width=\"1363\" height=\"740\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>The mathematical formula for sum:<\/p>\n<p>sum(n) = ( n*n + n )\/2 = n(n+1)\/2<\/p>\n<div id=\"themify_builder_content-538\" class=\"themify_builder_content themify_builder_content-538 themify_builder\" data-postid=\"538\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Write a recursive method for the sum of n integers? Recursion is a method calling itself. Such a method is said to be a recursive method. A recursive algorithm is similar to Mathematical Induction problems. We assume that there is a solution for a base case, and we will prove the result for the general [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1336","post","type-post","status-publish","format-standard","hentry","category-automation","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\/1336","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=1336"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1336\/revisions"}],"predecessor-version":[{"id":26424,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1336\/revisions\/26424"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}