- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes

- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages

- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation

- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples

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

In this article, we will understand how to find the sum of natural numbers using recursion. All possible positive numbers from 1 to infinity are called natural numbers. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.

Below is a demonstration of the same −

**Input**

Suppose our input is −

Enter the number : 25

**Output**

The desired output would be −

The sum of natural numbers up to 25 is 325

## Algorithm

Step 1 - START Step 2 - Declare 2 integer values namely my_input and my_sum. Step 3 - Read the required values from the user/ define the values Step 4 - A recursive function ‘Add’ is defined which takes an integer as input and returns the sum of the input value and its previous value until the input value is reduced to 0. Step 5 - The recursive function is called and the value ‘my_input’ is passed to it. Store the return value. Step 6 - Display the result Step 7 - Stop

## Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .

import java.util.Scanner; public class NaturalNumbers { public static void main(String[] args) { int my_input, my_sum; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number : "); my_input = my_scanner.nextInt(); System.out.println("The number is defined as " +my_input); my_sum = Add(my_input); System.out.println("The sum of natural numbers up to " + my_input + " is " + my_sum); } public static int Add(int my_input) { if (my_input > 0) return my_input + Add(my_input - 1); else return my_input; } }

## Output

Required packages have been imported A reader object has been defined Enter the number : 25 The number is defined as 25 The sum of natural numbers up to 25 is 325

## Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class NaturalNumbers { public static void main(String[] args) { int my_input, my_sum; my_input = 25; System.out.println("The number is defined as " +my_input); my_sum = Add(my_input); System.out.println("The sum of natural numbers up to " + my_input + " is " + my_sum); } public static int Add(int my_input) { if (my_input > 0) return my_input + Add(my_input - 1); else return my_input; } }

## Output

The number is defined as 25 The sum of natural numbers up to 25 is 325

- Related Articles
- C++ program to Find Sum of Natural Numbers using Recursion
- Golang Program to Find the Sum of Natural Numbers using Recursion
- Haskell Program to Find the Sum of Natural Numbers using Recursion
- Java Program to Find Sum of N Numbers Using Recursion
- How to Find Sum of Natural Numbers Using Recursion in Python?
- Java program to find the sum of n natural numbers
- Golang Program to Find the Sum of N Numbers using Recursion
- Haskell Program to Find Sum of N Numbers Using Recursion
- Java Program to Find the Product of Two Numbers Using Recursion
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Find Sum of Digits of a Number using Recursion
- How to Find the Sum of Natural Numbers using Python?
- Java Program to cube sum of first n natural numbers
- 8085 program to find the sum of first n natural numbers
- Java Program to Display Numbers and Sum of First N Natural Numbers