Subtraction of two numbers in C
In this post, you will learn the subtraction of two numbers using the C programming language. Such a type of question is generally asked in programming interviews. The interviewer may ask you to write a program to subtract two numbers using a particular method.
C Program to find subtraction of two numbers
In the below program, we have asked the user to enter two integers. Then, we calculated the subtraction of these two integers using the - operator and displayed it on the screen.
#include <stdio.h>
int main() {
int num1, num2, sub;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// calculating subtraction
sub = num1 - num2;
printf("%d - %d = %d", num1, num2, sub);
return 0;
}
Output of the above code:
Enter two integers: 60 22
60 - 22 = 38
C Program to subtraction two numbers using a function
Here, we have created a user-defined function subtraction() and written the subtraction logic. We are calling this function from the main function.
#include <stdio.h>
// Define function
int subtraction(int a, int b){
return a-b;
}
int main()
{
int num1, num2, total;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
//Calling the function
total = subtraction(num1, num2);
printf("Subtraction of the entered numbers: %d", total);
return 0;
}
Output of the above code:
Enter first number: 78
Enter second number: 21
Subtraction of the entered numbers: 57
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative