In this tutorial, we will discuss a concept of the Java program to find sum of two numbers using recurtion
In this article, we are going to learn how to find sum of two numbers using recursion in the Java programming language
Program
This program allows entering two digits to find the addition of two numbers using the recursive function in Java programming language
import java.util.Scanner; class AddTwoNum{ public static void main(String args[]){ int sum,x,y; //variable declaration //1 Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the value for x: "); x=scan.nextInt(); //2 System.out.print("Enter the value for y: "); y=scan.nextInt(); sum=add(x,y); //3,4 System.out.print("Sum of two numbers are:"+sum);//5 } static int add(int x, int y) //recursive method definition { if(y==0) return x; else return(1+add(x,y-1)); } }
When the above code is executed, it produces the following results
Enter the value for x: 25 Enter the value for y: 35 Summof two numbers are:60
Method
Similar post
Java code to the sum of two numbers
Java code to sum of two numbers using the method
Java program to sum of elements in an array
Suggested for you
if statements in Java language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…