C++語言簡單算術運算子示例


C++ 有 5 個基本的算術運算子。它們是:

  • 加法(+)
  • 減法(-)
  • 除法(/)
  • 乘法(*)
  • 取模(%)

這些運算子可以在 C++ 中對任何算術運算進行操作。我們來看一個示例:

示例

#include <iostream>
using namespace std;
main() {
   int a = 21;
   int b = 10;
   int c ;
   c = a + b;
   cout << "Line 1 - Value of c is :" << c << endl ;
   
   c = a - b;
   cout << "Line 2 - Value of c is  :" << c << endl ;
   
   c = a * b;
   cout << "Line 3 - Value of c is :" << c << endl ;
   
   c = a / b;
   cout << "Line 4 - Value of c is  :" << c << endl ;
   
   c = a % b;
   cout << "Line 5 - Value of c is  :" << c << endl ;
   return 0;
}

輸出

將輸出以下內容:

Line 1 - Value of c is :31
Line 2 - Value of c is  :11
Line 3 - Value of c is :210
Line 4 - Value of c is  :2
Line 5 - Value of c is  :1

更新於: 2020-02-11

16K+ 瀏覽量

開啟您的職業生涯

透過完成課程來獲得認證

開始
廣告