Java Program to Add All the Numbers from 1 to n by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Print ‘Hello World’ n times by Using Recursion

In this program we are going to see how to add all the numbers from 1 to n by using recursion by using Java programming language.

Java Program to Add All the Numbers from 1 to n by Using Recursion

Now let’s see different ways to add All the Numbers from 1 to n by Using Recursion.

Method-1: Java Program to Add All the Numbers from 1 to n By Using Static Input and Recursion

Approach:

  • Declare and initiate two static integer variables say count and sum with the value of 0.
  • Declare an integer variable n and assign any value to it.
  • Then call a user defined method calculateValue() and pass n as parameter.
  • Inside the user defined method add the value of updated count value into sum variable by using an If statement.
  • Call the same method inside that user defined method recursively.
  • Print the result.

Program:

class Main
{
    //Declare and initiate two static integer variables say count and sum with the value as 0. 
    static int count=0,sum=0;
    public static void main(String[] args)
    {
        int n=20;
        //calling the method
        calculateValue(n);
        System.out.print("sum of all number from 1 to "+n+" is= "+sum);
    }
    
    //define the method
    public static void calculateValue(int n)
    {
        //Increase the value of count by 1
        count++;
        //Check the condition whether the value of count is continuing till the target value reaches.
        if(count<=n)
        {
            //calculate the result
            sum=sum+count;
            //call the same function inside this user defined method recursively
            calculateValue(n);
        }
    }
}
Output:

sum of all number from 1 to 20 is= 210

Method-2: Java Program to Add All the Numbers from 1 to n By Using User Input and Recursion

Approach:

  • Declare and initiate two static integer variables say count and sum with the value of 0.
  • Declare an integer variable n and prompt the user to enter value for it by using Scanner class.
  • Then call a user defined method calculateValue() and pass n as parameter.
  • Inside the user defined method add the value of updated count value into sum variable by using an If statement.
  • Call the same method inside that user defined method recursively.
  • Print the result.

Program:

import java.util.Scanner;
class Main
{
    //Declare and initiate two static integer variable say count and sum with the value of 0. 
    static int count=0,sum=0;
    public static void main(String[] args)
    {
        //create object of scanner class.
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the value of n= ");
        //prompt the user to enter the value
        int n=sc.nextInt();
        //calling the method
        calculateValue(n);
        System.out.print("sum of all number from 1 to "+n+" is= "+sum);

    }
    
    //define the method
    public static void calculateValue(int n)
    {
        //Increase the value of count by 1
        count++;
        //Check the condition whether the value of count is continuing till the value reaches.
        if(count<=n)
        {
            //calculate the result
            sum=sum+count;
            //call the same function inside this user defined method recursively
            calculateValue(n);
        }
    }
}
Output:

Enter the value of n= 30
sum of all number from 1 to 30 is= 465

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: