C Language | Subtraction

Wednesday, 5 December 2012


On my previous topic, I was sharing about basic mathematics. I already show you how to add two integers and how to add a couple of integers being manipulated through your inputs and looping statements. Now, basic mathematics will not be complete without subtraction. Subtraction is the process of taking out certain amount from another amount. To understand more of this process I made a program that subtracts numbers. It also acts like a calculator.

This program, like the previous addition program I made, is used with a data type int because we're using integers, which needs to be whole numbers as inputs. Check out the codes below.


     1:  
   2:  #include<stdio.h>
   3:  #include<conio.h>
   4:   
   5:  int main()
   6:  {
   7:  int a,b,c;
   8:   
   9:  printf("Enter number: ");
  10:  scanf("%d", &a);
  11:   
  12:  printf("\nEnter number: ");
  13:  scanf("%d", &b);
  14:   
  15:  c = a - b;
  16:   
  17:  printf("\nThe difference is %d", c);
  18:  getch();
  19:  }


The program runs like this. At first it will ask for two (2) integers/numbers then it will be stored in variables a and b. After that, as seen above c = a - b; will execute the subtraction equation. After that the answer will be shown. Take note that variable a should hold the largest number to get exact positive answers. If variable a is less than variable b then you'll get the usual negative answers. Check out the sample outputs below.




Hope you enjoyed learning. We hope to hear from you. Thanks!
  

No comments:

Post a Comment

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...