Recursion in Java


Recursion is a powerful concept where a method calls itself to solve a problem.

Source Code

public class Main {
    public static int sum(int k) {
        if (k > 0) {
            return k + sum(k - 1);
        } else {
            return 0;
        }
    }
    public static void main(String[] args) {
        int result = sum(10);
        System.out.println(result);
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments