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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.