Arithmetic

In C, basic arithmetic operations such as addition, subtraction, multiplication, and division can be performed using a set of built-in operators. C also supports more advanced arithmetic operations such as modulo (remainder) and increment/decrement.

Here are some examples of basic arithmetic operations in C:

#include <stdio.h>

int main(void) {
  int a = 10;
  int b = 5;

  int sum = a + b;        // addition
  int diff = a - b;       // subtraction
  int product = a * b;    // multiplication
  int quotient = a / b;   // division
  int remainder = a % b;  // modulo (remainder)

  printf("Sum: %d\n", sum);
  printf("Difference: %d\n", diff);
  printf("Product: %d\n", product);
  printf("Quotient: %d\n", quotient);
  printf("Remainder: %d\n", remainder);

  return 0;
}
In this example, the variables a and b are initialized with the values 10 and 5, respectively. Five arithmetic operations are performed on these variables and the results are stored in separate variables. The results are then printed to the screen using the printf function.

C also supports the increment (++) and decrement (--) operators, which can be used to increase or decrease the value of a variable by 1. Here is an example of how these operators can be used:

#include <stdio.h>

int main(void) {
  int num = 5;
  printf("num: %d\n", num);  // prints 5
  num++;  // increases the value of num by 1
  printf("num: %d\n", num);  // prints 6
  num--;  // decreases the value of num by 1
  printf("num: %d\n", num);  // prints 5
  return 0;
}
In this example, the num variable is initialized with the value 5 and is then incremented and decremented using the ++ and -- operators. The resulting values are printed to the screen using the printf function.

Last modified on: 2023-01-10 00:45:31