Subtract two numbers
Table of Contents
In this tutorial, we will discuss the C program to subtraction of two numbers
In this post, we are going to learn how to find subtraction of two numbers via different 6 ways
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1=78,num2=54;//variable declaration
int sub=num1-num2;//caculation
printf("subtraction of %d - %d : %d\n",num1,num2,sub);
//display the output
getch();
return 0;
} When the above code is executed, it produces the following result
subtraction of 78 - 54: 24
Approach
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2,sub;//variable declaration
printf("Enter the first number: ");
scanf("%d",&num1);//read value for num1
printf("Enter the second number: ");
scanf("%d",&num2);//read value for num2
sub=num1-num2;//calculate the subtraction
printf("subtraction of %d - %d : %d\n",num1,num2,sub);
//display the result of subtraction
getch();
return 0;
} When the above code is executed, it produces the following result
case 1
Enter the first number: 345 Enter the second number: 125 subtraction of 345-125: 220
case 2
Enter the first number: 150 Enter the second number: 245 subtraction of 345-125: -95
Approach
Program 3
#include <stdio.h>
#include <stdlib.h>
int sub(int a,int b);//function declaration
int main()
{
int num1,num2,result;//variable declaration
printf("Enter two number to subtract\n");
scanf("%d%d",&num1,&num2);
//takes input from the user
result=sub(num1,num2);//calling the function
printf("Subtraction of two numbers= %d",result);
getch();
return 0;
}
int sub(int a, int b){//function definition
int c=a-b;
return (c);
} When the above code is executed, it produces the following result
Enter two number to subtract 116 44 Subtraction of two numbers=72
Approach
Program 4
#include <stdio.h>
#include <stdlib.h>
int sub(int,int);//function declaration
int main()
{
int num1,num2,result;//variable declaration
printf("Enter two number to subtract\n");
scanf("%d%d",&num1,&num2);
//takes input from the user
result=sub(num1,num2);//calling the function
printf("Subtraction of two numbers= %d",result);
getch();
return 0;
}
int sub(int num1, int num2){//function definition
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 number to subtract 345 123 Subtraction of two numbers=222
Approach
Program 5
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p1,*p2;//pointer variable declaration
int num1,num2,result;//variable declaration
printf("Enter two number to subtract\n");
scanf("%d%d",&num1,&num2);
//takes input from the user
p1=&num1;
p2=&num2;
result=*p1-*p2;
printf("Subtraction of two numbers= %d",result);
getch();
return 0;
} When the above code is executed, it produces the following result
Enter two number to subtract 589 478 Subtraction of two numbers=111
Approach
Program 6
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2,sub;//variale declaration
//input first number
printf("Enter the first number\n");
scanf("%d",&num1);//read first number
//input second number
printf("Enter the second number\n");
scanf("%d",&num2);//read second number
sub=num1+~num2+1;//calculate the subtraction
printf("subtraction of %d - %d = %d\n",num1,num2,sub);
//display the result
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter the first number 87 Enter the second number 23 subtraction of 87 - 23 = 64
Approach
Similar post
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.