Write a Java Program to Find the Sum of Natural Numbers using Recursion

Java Program to Find the Sum of Natural Numbers using Recursion

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Find the Sum of Natural Numbers using Recursion
Java Program to Find the Sum of Natural Numbers using Recursion

Write a Java Program to Find the Sum of Natural Numbers using Recursion

PROGRAM: Java Program to Find the Sum of Natural Numbers using Recursion

/*Java Program to Find the Sum of Natural Numbers using Recursion*/

public class AddNumbers {

public static void main(String[] args) {
int number = 20;
int sum = addNumbers(number);
System.out.println(“Sum = ” + sum);
}

public static int addNumbers(int num) {
if (num != 0)
return num + addNumbers(num – 1);
else
return num;
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *