Snippets Collections
// Time Complexity : O(n), Space Complexity : O(n)

import java.io.*;
import java.util.*;

class GFG 
{
	static int getSum(int n)
	{
		if(n == 0)
			return 0;

		return n + getSum(n - 1);
	}
    public static void main(String [] args) 
    {
    	int n = 4;
    	
    	System.out.println(getSum(n));
    }
}
star

Sun Feb 06 2022 21:09:41 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #recursion #naturalsum

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension