Categories: category

Java program to find the sum of natural numbers using recursion

Java program to find the sum of natural numbers using recursion

In this tutorial, we will discuss a concept of  Java program to find the sum of natural numbers using recursion

In this article, we are going to learn  how to  find the sum of natural numbers using recursion in the Java programming language

Find the sum of natural numbers

What are Natural numbers

The positive integer numbers 1,2,3,4….n are known as natural numbers

 

Program

This program allows to enter a number to find addition of natural numbers from 1 to given number using recursive function in Java programming language

import java.util.Scanner;
class SumOfNum1{
public static void main(String args[]){
    int sum;  //variable declaration
    Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the value for num: ");
int num=scan.nextInt();
     sum=addNumbers(num);
   System.out.print("Sum of natural numbers are:"+sum);
} 
static int addNumbers(int num)
{
    
    if(num!= 0)
        return num+addNumbers(num-1);
    else
        return num;
}
}

When the above code is executed, it produces the following results

Enter a value for num: 50
Sum of natural numbers are: 1275

In the above program , the number entered by the user is passed to the addNumber() function as an argument.

In the above case, when the user enters 50 as an argument, 50 is passed to function. Subsequently, until “if” statement returns the true value, value decreases by one in every step. Finally, when the value becomes 0, “if” statement returns as false and the “else” part is executed.

Therefore, function return with decreasing value to calculate the result e.g 1+2+3+4+5…..48+49+50=1275

 

Similar post

Java program to find the sum of natural numbers using loops

Python program to Calculate the sum of natural numbers using loops

C++ program to calculate the sum of natural numbers using loops

C program to calculate the sum of natural numbers using loops

 

C program to find the sum of natural numbers using recursion

Python program to Calculate the sum of natural numbers using recursion

C++ program to calculate the sum of natural numbers using recursion

 

 

Suggested for you

Java if-else statements

Java recursive method

Java method

 

Calculate the sum of natural numbers in Python
Python program to find the sum of natural numbers using recursion
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago