微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#程序执行所有基本算术运算

C#程序执行所有基本算术运算

<p><p style="max-width:90%"table table-bordered"><thead><tr><th style="width: 16.5253%; text-align: center;">运算符</p><p></th><th style="width: 83.4747%; text-align: center;">描述</p><p></th></tr></thead><tbody><tr><td style="width: 16.5253%; text-align: center;" valign="top">+</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">将两个操作数相加</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">-</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">从第一个操作数中减去第二个操作数</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">*</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">将两个操作数相乘</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">/</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">将分子除以分母</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">%</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">取模运算符和整数除法后的余数</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">++</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">递增运算符将整数值增加一</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">--</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">递减运算符将整数值减少一</p><p></td></tr></tbody></table><p style="">要进行加法运算,使用加法运算符 −</p>
num1 + num2;

同样地,它适用于减法、乘法、除法和其他运算符。

示例

让我们看一个完整的示例来学习如何在C#中实现算术运算符。

实时演示

using System;
namespace Sample {
   class Demo {
      static void Main(string[] args) {
         int num1 = 50;
         int num2 = 25;
         int result;
         result = num1 + num2;
         Console.WriteLine("Value is {0}", result);
         result = num1 - num2;
         Console.WriteLine("Value is {0}", result);
         result = num1 * num2;
         Console.WriteLine("Value is {0}", result);
         result = num1 / num2;
         Console.WriteLine("Value is {0}", result);
         result = num1 % num2;
         Console.WriteLine("Value is {0}", result);
         result = num1++;
         Console.WriteLine("Value is {0}", result);
         result = num1--;
         Console.WriteLine("Value is {0}", result);
         Console.ReadLine();
      }
   }
}

输出

Value is 75
Value is 25
Value is 1250
Value is 2
Value is 0
Value is 50
Value is 51

以上就是C#程序执行所有基本算术运算的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐