C# 判断一个数是否能被2整除的程序

WBOY
发布: 2023-09-05 22:53:06
转载
1180 人浏览过

C# 判断一个数是否能被2整除的程序

如果这个数字除以 2 的余数是 0,那么它就能被 2 整除。

假设我们的数字是 5 ,我们将使用以下 if-else 来检查它 -

// checking if the number is divisible by 2 or not
if (num % 2 == 0) {
   Console.WriteLine("Divisible by 2 ");
} else {
   Console.WriteLine("Not divisible by 2");
}
登录后复制

示例

下面是一个判断数字是否能被2整除的示例。

现场演示< /p>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         int num;
         num = 5;
         // checking if the number is divisible by 2 or not
         if (num % 2 == 0) {
            Console.WriteLine("Divisible by 2 ");
         } else {
            Console.WriteLine("Not divisible by 2");
         }
         Console.ReadLine();
      }
   }
}
登录后复制

输出

Not divisible by 2
登录后复制

以上是C# 判断一个数是否能被2整除的程序的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!