C++ | Subtraction of numbers

Introduction

First program will directly calculate the subtraction which is not flexible. So the second program is introduced which will let you enter the numbers from keyboard and calculate the subtraction of it. The later programs are modified forms of second program and lets you add more variables to the program and lets you calculate subtraction of more numbers. The same way you can extend your program. Go enjoy the program. Lets begin……

First program

``````
#include<iostream.h>
#include<conio.h>
void main()
{
//clears the screen.
clrscr();
//declare variable type int
int a,b,c;
//The two numbers are declared which makes program non-flexible.
a=12;
b=10;
//calculates subtraction and save them in 'c'
c=a-b;
//show the output c
cout<<"Subtraction of two numbers are  "<<c;
//get character
getch();
}
``````

output will be

Subtraction of two numbers are 2.

Second program

``````
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b,c;
//Input the two numbers and save them in 'a' and 'b'
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
//calculate sub and save them in 'c'
c=a-b;
//show the output c
cout<<"Sub of two numbers are "<<c;
//get character
getch();
}
``````

output will be

Enter the first no.
12
Enter the second no,
8
Sub of two numbers are 4.

Third program

``````
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b,c,d;
//Input the three numbers and save them in 'a' and 'b' and 'c'.
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
cout<<"Enter the third no."<<endl;
cin>>c;
//calculate sub and save them in 'd'
d=a-b-c;
//show the output d
cout<<"Sub of three numbers are "<<d;
//get character
getch();
}
``````

output will be

Enter the first no.
30
Enter the second no.
10
Enter the third no.
10

Sub of three numbers are  20.

Fourth program

``````
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b,c,d,e;
//Input the four numbers and save them in 'a' , 'b','c' and'd'
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
cout<<"Enter the third no."<<endl;
cin>>c;
cout<<"Enter the fourth no."<<endl;
cin>>d;
//calculate sub and save them in 'e'
e=(a+b)-(c+d);
//show the output e
cout<<"Sub of four numbers are "<<e;
//get character
getch();
}
``````

output will be

Enter the first no.
10
Enter the second no.
20
Enter the third no.
10
Enter the fourth no.
20
Sub of four numbers are 0.

How does it work.

1. you enter the numbers that are saved in their respective variables.
2. all are calculated and saved in one variable.
3. sub is displayed on screen.

Extending it.

You can extend it by adding number of variables and their cout and cin and modifying subtraction variable.

Explanation.

1. Include ‘iostream.h’ and ‘conio.h’ files.
3. Start program by first clearing the screen.
4. Declare the variables as int (name the as you want.)
5. Add cout and cin of variables you want to subtract.(as shown in program)
6. Calculate the sub of  the variables and save them in one variable.
7. Cout the sub variable.

At the end

You learnt creating the c++ program of subtraction of numbers. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Sharing options: