Calculations

Use of C++ program to find sum of two numbers using recursion

Use of C++ program to find sum of two numbers using recursion

In this tutorial, we will discuss a concept of the  Use of C++ program to find sum of two numbers using recursion

In this article, we are going to learn  how to  find the addition of two numbers using recursion in the C++ programming language

Find sum of two numbers

Program

This program allows entering two digits from the user to find the addition of two numbers using the recursive function in C++ programming language

#include <iostream>
#include <conio.h>
using namespace std;

int add(int,int);
int main()
{
    int x,y,result;  //variable declaration
    cout<<"enter two integers: ";
    cin>>x>>y;
     result=add(x,y);
   cout<<"Sum of two numbers are:"<<result;
    getch();
    return 0;
}
int add(int x, int y)
{
    if(y==0)
        return x;
    else
        return(1+add(x,y-1));
}

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

Enter two integers: 12
34
Sum of two numbers are: 46

Method

  1. Declare the three int type variables x,y and result. x and y are used to receive input from the user whereas the result 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 result
  5. Display the result on the screen.

 

 

 

Similar post

C++ code to the sum of two numbers

C++ code to sum of two numbers using the function

C++ program to sum of numbers in an array

 

Suggested for you

The operator in C++ language

recursion in C++ language

if statements in C++ language

 

Calculate power of a number using recursion in Java
C program to calculate the sum of natural numbers
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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago