หากต้องการพิมพ์ตัวเลขที่หารด้วย 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