Subtractions of numbers in C-Language:
Q:- How to Subtract two constant numbers?
Answer:
- #include <stdio.h>
- int main() {
- const int a=5;
- const int b=2;
- int res;
- res=a-b;
- printf("Answer is %d",res);
- return 0;
- }
Code Explanation:
First line we include preprocessing directory.
Second line we use main() function.
Third and fourth line we define a constant variable which can not be change.
Fifth line we create an 'res' variable with integer data type.
Sixth line we use 'res' variable to store subtraction of 'a' from 'b'.
Seventh line we print the 'res' variable.
Eight line we end the program with return value '0'.
Ninth line we close the curly braces which we open in second line.
output:
Note: You use constant so you can not change your constant values during runtime.
Q:- How to subtract two numbers by taking input from user?.
- #include <stdio.h>
- int main() {
- int a,b,res;
- printf("Enter First Number:");
- scanf("%d",&a);
- printf("Enter Second Number:");
- scanf("%d",&b);
- res=a-b;
- printf("Sum of two numbers is %d",res);
- return 0;
- }
Code Explanation:
First line we include preprocessing directory.
Second line we use main() function.
Third line we define three variable in the integer data type.
Fourth line we ask from user to enter first number.
Fifth line we get 1st number from the user.
Sixth line we ask from user to enter Second number.
Seventh line we get 2nd number from the user.
Eighth line we use 'res' variable to store subtract 'a' from 'b'.
Ninth line we print the 'res' variable.
Ten line we end the program with return value '0'.
Eleven line we close the curly braces which we open in second line.
Output:
Note:If your program reads input, please enter the inputs in the STDIN box above or try to enable the "Interactive" mode option above.
Check your program in compiler below
This is the code for subtracting two numbers .You can also use this code for subtraction of more than two number only for few changes.