Find elements

Use of Java program to find sum of two numbers using recursion

Java program to find sum of two numbers using recursion

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

find sum of two numbers

 

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

  1. Declare the three int type variables x,y and sum. x and y are used to receive input from the user whereas the sum is used to assign the output.
  2. Receive input from the user for x, y to perform addition.
  3. When the function is called, two numbers will be passed as an argument. Subsequently,  the sum of the two numbers will be found.
  4. Then, assign the output to the variable sum
  5. Display the result on the screen.

 

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

Method in Java language

The operator in Java  language

recursion in Java language

if statements in Java language

 

 

Program to print even or odd numbers in given range using recursion
C 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

C# inverted full pyramid star pattern

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

3 weeks ago

C# Full Pyramid star pattern program

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

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago