C

Examples Of Arithmetic Operators In C

To perform arithmetic operations on the operands, arithmetic operators are used. x + y, for example, is an addition arithmetic operation in which x and y are operands and the + symbol is an arithmetic operator.

1. Example of the Addition(+) Operator

It introduces two new operands. In the following example, we have two integer variables and use the + operator to find the sum of these numbers.

#include <stdio.h>
int main ()
{
  int x = 100, y =20;
  int sum;

  //Addition(+) operator
  //The addition result is stored in variable sum
  sum = x+y;

  //print result
  printf("Sum of %d and %d is: %d",x, y, sum);

  return 0;
}

Output:

Sum of 100 and 20 is: 120

2. Example of the Subtraction(-) Operator

Subtraction is performed with the – operator. We are subtracting the value of y from the value of x in this case. At the end of the program, the subtraction result is stored in an int variable and displayed using printf().

#include <stdio.h>
int main ()
{
  int x = 100, y =20;
  int sub;

  //Subtraction(-) operator
  //The result is stored in "sub"
  sub = x-y;

  //print subtraction result
  printf("x - y is: %d", sub);

  return 0;
}

Output:

x - y is: 80

3. Example of the Multiplication(*) Operator

The * operator is used to multiply. As shown in the following program, it multiplies two operands.

#include <stdio.h>
int main ()
{
  int x = 100, y =20;
  int product;

  //Multiplication(*) operator
  product = x*y;

  //print multiplication result
  printf("Multiplication of %d and %d is: %d", x, y, product);

  return 0;
}

Output:

Multiplication of 100 and 20 is: 2000

4. Division(/) Operator Exemplification

The / operator returns the quotient value after dividing the left and right operands. For instance, if 10 is divided by 5, the / operator will return 2. (quotient).

#include <stdio.h>
int main ()
{
  int x = 100, y =20;
  int quotient;

  //Division(/) operator, it returns the quotient after
  //dividing x by y
  quotient = x / y;

  //print Division result
  printf("Quotient value after dividing %d by %d is: %d", x, y, quotient);

  return 0;
}

Output:

Quotient value after dividing 100 by 20 is: 5

5. Example of a Modulus (%) Operator

The remainder after dividing the left-side operand by the right-side operand is returned by the % operator. 10% 2 would, for example, return 0 because 10 is perfectly divisible by 2.

#include <stdio.h>
int main ()
{
  int x = 100, y =20;
  int rem;

  //Modulus(%) operator, it returns the remainder after
  //dividing x by y
  rem = x % y;

  //print Modulus result
  printf("Remainder value after dividing %d by %d is: %d", x, y, rem);

  return 0;
}

Output:

Remainder value after dividing 100 by 20 is: 0

Related Articles

Leave a Reply

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

Back to top button