Categories: category

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 use of C program to find the sum of two numbers using recursion

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

Sum of two numbers

Program

This program allows the entry of two digits from the user and to find the sum of two numbers using the recursive function in C programming language

#include <stdio.h>
#include <stdlib.h>
int add(int,int);  //Function prototype        
int main()
{
    int x,y,result;  //variable declaration
    printf("enter two numbers: ");
    scanf("%d %d",&x,&y);
     result=add(x,y);//function call
    printf("Sum of two numbers are: %d\n",result);
    getch();
    return 0;
}
int add(int x, int y)     //function declaration(recursive function)
{
    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 numbers: 23
34
sum of two numbers are:57

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 program to sum of two numbers using the function

C program to calculate sum of array elements

 

Suggested for you

Function in C language

The operator in C  language

recursion in C language

if statements in C language

 

Calculate average of odd and even numbers in Java
Python program to find the sum of two 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.

View Comments

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…

2 months 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…

2 months ago