यह पता लगाने के लिए कि कोई संख्या 2 से विभाज्य है या नहीं, जांचें कि क्या होता है जब संख्या 2 से विभाज्य होती है।
यदि शेषफल 0 है, तो संख्या 2 से विभाज्य है, अन्यथा यह असत्य है -
if (num % 2 == 0) {
Console.WriteLine("Divisible by 2 ");
} else {
Console.WriteLine("Not divisible by 2");
} निम्नलिखित पूरा कोड है -
उदाहरण
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
class Test {
static void Main(string[] args) {
int num;
num = 18;
// 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();
}
}
} आउटपुट
Divisible by 2