Subtraction of 2 Numbers: C


In this video tutorial you can learn the procedure followed in C programming to subtract two numbers.

Related Read:

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b, c;  
  7.   
  8.     printf("Enter value of a and b\n");  
  9.     scanf("%d%d", &a, &b);  
  10.   
  11.     c = a - b;  
  12.       
  13.     printf("Subtraction of %d and %d is %d\n", a, b, c);  
  14.   
  15.     return 0;  
  16. }  

Output:
Enter value of a and b
30
20
Subtraction of 30 and 20 is 10

You can write same program without using third variable to calculate subtraction of 2 numbers, as below:

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b;  
  7.   
  8.     printf("Enter value of a and b\n");  
  9.     scanf("%d%d", &a, &b);  
  10.   
  11.     printf("Subtraction of %d and %d is %d\n", a, b, (a+b));  
  12.   
  13.     return 0;  
  14. }  

Output:
Enter value of a and b
30
20
Subtraction of 30 and 20 is 10

Note: Instead of int you can take float variables too. That would help in taking both integer as well as real values from the user as input.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Subtraction of Two Numbers: C Programming



YouTube Link: https://www.youtube.com/watch?v=2cAMGtToJNE [Watch the Video In Full Screen.]


This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *