Subtractions of numbers in C-Language

 Subtractions of numbers in C-Language:


Q:- How to Subtract two constant numbers?

code




Answer:

  1. #include <stdio.h>
  2. int main() {
  3.        const int a=5;
  4.        const int b=2;
  5.        int res;
  6.        res=a-b;
  7.        printf("Answer is %d",res); 
  8.        return 0;
  9. }
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:
Result

Check your program in compiler below



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?.



Coding



Answer:

  1. #include <stdio.h> 
  2. int main() {
  3.        int a,b,res;
  4.        printf("Enter First Number:");
  5.        scanf("%d",&a);
  6.        printf("Enter Second Number:");
  7.        scanf("%d",&b);
  8.        res=a-b;
  9.        printf("Sum of two numbers is %d",res);
  10.        return 0;
  11. }
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:

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.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.