Go ahead, jump. 100,000 lemmings can't be wrong.
   Go ahead, jump. 100,000 lemmings can't be wrong. Thursday - 21st August, 2008 - 00:29:22 

Site Menu  
 
Home
Web News
Reviews
Previews
Guides
Case Gallery
Contact Us
About Us
Sponsors
Links
 
     

  Visitors  
   
     

   Guide : Fundamental C Programming (Rev 1.0) »  
 

 

Fundamental C Programming (Rev 1.0) - Arithmetic Operations
   
 Date  : Feb 17th, 2001
 Category  : Programming
 Manufacturer   : N/A
 Author  : Jin-Wei Tioh
In our earlier program, we used the addition operator ( + ) to add two integers. There are other arithmetic operators in C, which are listed below :

Operation Operator C Expression Algebraic Expression
Addition + a + b a + b
Subtraction - a - b a - b
Multiplication * a * b ab
Division / a / b a / b
Modulus % a % b a mod b

One important note though, integer division yields an integer result. Eg. 2/3 yields 0, instead of 0.666666667. Similarly, 100/3 would yield 33 instead of 33.33333333. This is because we are storing the results of the division into a variable of type int, and only the integer portion of the result (ie. the part before the decimal point) is stored. The rest is simply ignored. This is what is called truncation.

So how would we write a program to read in two numbers (be they integers of real numbers), perform division on them and print the result to the display? Simple, just modify our earlier program.

Code :

/* Number division program */

#include <stdio.h>

main() {

/* Variable declaration */
float number1, number2, sum;

printf("First integer\t: ");
scanf("%f", &number1);
printf("Second integer\t: ");
scanf("%f", &number2);

sum = number1 / number2;
printf("The sum of %.2f and %.2f is %.2f\n", number1, number2, sum);
return 0;

}

Output :

Only a few things have changed. First, in our declaration statement, we changed the type of variables declared to float. floats are allocated much more space than ints, allowing them to store real numbers such as 1.23456789, which is exactly what we need in our program.

Secondly, the %d conversion specifiers have been changed to %f. This specifies that the data should be a real number. We have also changed the addition operator ( + ) to the division operator ( /) to divide number1 by number2 and store the results in sum.

Lastly, the conversion specifier in the last printf statement have been changed to %.2f. Why %.2f and not %f? The .2 indicates the digits of precision that we want. If our program divided 10 by 3 (ie. 10/3), the result would be printed out as 3.33 (2 digit precision) instead of 3.3333333 (arbitrary digits of precision).

Well, that concludes Part 2 of the programming guide. A lot of terminologies and syntax have been introduced, so take your time to familiarize yourself with them. Also, try writing variations or completely different programs to practice. Stay tuned for the next article. Until then, have fun...

 Print this article Home

 E-mail this article

 Discuss this article

 
 
     


Copyright © 2000-2005 BlueSmoke. All rights reserved. Terms, Conditions and Privacy Information.
Site Design by Jin-Wei Tioh

Sitemap