C Tutorial/Operator/Arithmetic Operators

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Arithmetic operators in action

  1. Arithmetic operators: +, -, *, \ and the modulus operator %.
  2. % cannot be used for float data type or double data type.


#include<stdio.h>
main(){
  int a = 1,b = 2,c = 3,d = 4;
  int sum,sub,mul,rem;
  float div;
  sum = b+c;
  sub = b-c;
  mul = b*c;
  div = b/c;
  rem = b%d;
  a = b/c * d;
  printf("\n sum = %d, sub = %d, mul = %d, div = %f",sum,sub,mul,div);
  printf("\n remainder of division of b & d is %d",rem);
  printf("\n a = %d",a);
}

Common Arithmetic Operators

Operator Description Example

Multiplication Result = Operand1 * Operand2; / Division Result = Operand1 / Operand2; % Modulus (remainder) Remainder = Operand1 % Operand2; + Addition Result = Operand1 + Operand2; - Subtraction Result = Operand1 -Operand2;

Using the Addition operator

#include <stdio.h>
main(){
  int iOperand1 = 0;
  int iOperand2 = 0;
  int iResult = 0;
  printf("\nEnter first operand: ");
  scanf("%d", &iOperand1);
  printf("Enter second operand: ");
  scanf("%d", &iOperand2);
  iResult = iOperand1 + iOperand2;
  printf("The result is %d\n", iResult);
}
Enter first operand: 1
      Enter second operand: 2
      The result is 3