Calculations

C++ program to subtract two number using Function

C++ program to subtract two number using Function

In this tutorial, we will discuss the C++ program to subtract two numbers using the function

In this topic, we are going to learn how to subtract two numbers (integer, floating point) using the 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

subtract two number using Function

Approach

  • Function declaration or prototype
  • Variable declaration
  • Read value from user
  • function definition
  • call the function
  • display result on the screen

 

Code to subtract two integer number using Function

The program allows to enter two integer values then, it calculates subtraction of the given numbers

Program 1

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

int subNum(int a, int b);//Function prototype
int main()
{
    int result,num1,num2;
    cout<<"Enter two numbers to subtract\n";
    cin>>num1>>num2;//read input from the user

    result=subNum(num1,num2);//call the function
     cout<<"subtraction of given two numbers: "<<result;
     getch();
    return 0;
}
int subNum(int x, int y)//function definition
{
    int c=x-y;
    return (c);
}

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

case 1

Enter two numbers to subtract
678
456
subtraction of given two numbers: 222

 

case 2

Enter two numbers to subtract
46
789
subtraction of given two numbers: -743

 

Code to subtract two floats number using Function

The program allows to enter two float values then, it calculates subtraction of the given numbers

Program 2

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

float subNum(float a, float b);//Function prototype
int main()
{
    float result,num1,num2;//variable declaration
    cout<<"Enter two numbers to subtract\n";
    cin>>num1>>num2;//read input from the user

    result=subNum(num1,num2);//call the function
     cout<<"subtraction of given two numbers: "<<result;
     getch();
    return 0;
}
float subNum(float x, float y)//finction definition
{
    float c=x-y;
    return (c);
}

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

case 1

Enter two numbers to subtract
233.4
34.5
subtraction of given two numbers: 198.9

 

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 C++ language

Subtract two numbers in Python language

 

 

C program to subtract two number using Function
Python program to subtract two number using Function
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…

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