用於列印給定數字可以被 3 和 5 整除的所有數字的 C# 程式


要列印可以被 3 和 5 整除的數字,請使用 && 運算子並檢查兩個條件 -

f (num % 3 == 0 && num % 5 == 0) {}

如果上述條件為真,那就意味著該數字可以被 3 和 5 整除。

以下是完整程式碼 -

示例

 即時演示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         int num;
         num = 15;

         Console.WriteLine("Number: "+num);

         // checking if the number is divisible by 3 and 5
         if (num % 3 == 0 && num % 5 == 0) {
            Console.WriteLine("Divisible by 3 and 5");
         } else {
            Console.WriteLine("Not divisible by 3 and 5");
         }
         Console.ReadLine();
      }
   }
}

輸出

Number: 15
Divisible by 3 and 5

已更新於:2020 年 6 月 20 日

5K+ 瀏覽量

開啟你的 職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.