C語言算術運算示例程序

基本算術運算包括加法,減法,乘法和減法。它們一般對數字數據類型執行基本算術運算,如:intfloatdouble

加法操作

下面給出的示例代碼解釋了C語言中的加法運算符:

#include <stdio.h>

int main() {
   int op1, op2, sum;      // variable declaration

   op1 = 5;                // variable definition
   op2 = 3;
   sum = op1 + op2;        // addition operation
   printf("sum of %d and %d is %d", op1, op2, sum);
}

執行上面示例代碼,得到以下結果 -

sum of 5 and 3 is 8

減法運算

C語言中的減法運算,如下面給出的示例代碼 -

#include <stdio.h>

int main() {
   int op1, op2, sub;      // variable declaration
   op1 = 5;                // variable definition
   op2 = 3;
   sub = op1 - op2;        // subtraction operation
   printf("Output of %d − %d is %d", op1, op2, sub);
}

執行上面示例代碼,得到以下結果 -

Output of 5 − 3 is 2

乘法運算

下面給出的示例代碼說明了C語言中的乘法。星號(*)用作乘法運算符。

#include <stdio.h>

int main() {
   int op1, op2, mul;      // variable declaration

   op1 = 5;                // variable definition
   op2 = 3;

   mul = op1 * op2;        // multiplication operation

   printf("Output of %d multiplied by %d is %d", op1, op2, mul);
}

執行上面示例代碼,得到以下結果 -

Output of 5 multiplied by 3 is 15

除法運算

下面給出的示例代碼說明了C語言中的除法運算。如下代碼所示 -

#include <stdio.h>

int main() {
   int op1, op2, div;      // variable declaration
   op1 = 6;                // variable definition
   op2 = 3;

   div = 6 / 3;            // division operation

   printf("Output of %d divide by %d is %d", op1, op2, div);
}

執行上面示例代碼,得到以下結果 -

Output of 6 divide by 3 is 2