Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Add All the Numbers from 1 to n by Using Recursion
In this program we are going to see how to add all the numbers between a to b by using recursion where a and b are given by using Java programming language.
Java Program to Add All the Numbers between a to b by Using Recursion
Now let’s see different ways to add All the Numbers between a to b by Using Recursion where a and b are given.
Method-1: Java Program to Add All the Numbers between a to b By Using Static Input and Recursion
Approach:
- Declare and initiate two static integer variables say
count
andsum
with the value of 0. - Declare and initiate two integer variable
a
andb
and assign any value to it. - Update the
count
value bya
. - Define an user defined method
calculateValue()
and passb
as parameter. - Inside the user defined method add the value of updated
count
value intosum
variable by using an If statement. - Call the same method inside that user defined method recursively till it reaches its target.
- Print the result.
Program:
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) { int a=20; int b=30; count=a; //calling the method calculateValue(b); System.out.print("Sum of all number between "+a+" to " +b+" is= "+sum); } //define the method public static void calculateValue(int b) { //Increase the value of count by 1 count++; //Check the condition whether the value of count is continuing till the value reaches. if(count<b) { //calculate the result sum=sum+count; //call the same method recursively calculateValue(b); } } }
Output: Sum of all number between 20 to 30 is= 225
Method-2: Java Program to Add All the Numbers between a to b By Using User Input and Recursion
Approach:
- Declare and initiate two static integer variables say
count
andsum
with the value of 0. - Declare and initiate two integer variable
a
andb
and take the values of both as user input by using Scanner class. - Update the
count
value bya
. - Define an user defined method
calculateValue()
and passb
as parameter. - Inside the user defined method add the value of updated
count
value intosum
variable by using an If statement. - Call the same method inside that user defined method recursively till it reaches its target.
- 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 a= "); //prompt the user to enter the value int a=sc.nextInt(); System.out.print("Enter the value of b= "); //prompt the user to enter the value int b=sc.nextInt(); count=a; calculateValue(b); System.out.print("sum of all number between "+a+" to " +b+" is= "+sum); } //define the method public static void calculateValue(int b) { //Increase the value of count by 1 count++; //Check the condition whether the value of count is continuing till the value reaches. if(count<b) { //calculate the result sum=sum+count; //call the same function inside this user defined method calculateValue(b); } } }
Output: Enter the value of a= 30 Enter the value of b= 50 sum of all number between 30 to 50 is= 760
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: