Calculations

Use of C program to subtraction of two numbers using recursion

Use of C program to subtraction of two numbers using recursion

In this tutorial, we will discuss the Use of C program to subtraction of two numbers using the recursion

In this topic, we are going to learn how to subtract two numbers (integer )using the recusive function in C language

already we are learned the same concept using the operator

if you want to remember click here, C program to subtract two numbers

subtraction of two numbers using recursion

program to subtraction of two numbers using recursion

The program allows to enter two integer numbers and then it calculates the subtraction of the given numbers using recursive function of C language

Program 1

#include <stdio.h>
#include <stdlib.h>
 int sub(int,int);//function prototype
int main()
{
    int num1,num2;//declare te variables
    printf("Enter two numbers: \n");
    scanf("%d%d",&num1,&num2);//read te value input from user
     printf("Subtraction of two numbers : %d",sub(num1,num2));//call the function
     getch();
    return 0;
}
int sub(int num1, int num2)//define the recursive function
{
    if(num2==0)
        return num1;
    else
        return sub((num1-1),(num2-1));

}

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

Enter two numbers:
1200
500
Subtraction of two numbers : 700

 

Approach

  • Declare three int type variables num1,num2. num1,num2 are used to received input from the user
  • Receive input from the user for num1,num2 to perform subtraction
  • Define a recursive function to subtract two numbers
  • When the function is called, two numbers will be passed as argument subsequently the subtract of two numbers will be found
  • display the result on the screen

 

Suggetsed post

Operator in C language

Data type in C language

Variable in C language

 

Similar post

Subtract two numbers in Java language

Subtract two numbers in C++ language

Subtract two numbers in Python language

 

Subtract two numbers in Java language using method

Subtract two numbers in C language using function

Subtract two numbers in C++ language using function

Subtract two numbers in Python language using function

Use of C++ program to subtraction of two numbers using recursion
Calculate electricity bill using OOP in Java
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

  • Hi, I would like to subscribe for this website to take newest updates, therefore where can i do it please help. Suzanne Talbot Valeda

Recent Posts

PHP Star Triangle pattern program

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

6 months ago

PHP Full Pyramid pattern program

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

6 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…

6 months ago

Python Full Pyramid star pattern program

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

9 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…

1 year 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,…

1 year ago