सबसे पहले, दो नंबर सेट करें।
int one = 250; int two = 200;
अब उन नंबरों को निम्न फ़ंक्शन में पास करें।
public int RemainderFunc(int val1, int val2) {
if (val2 == 0)
throw new Exception("Second number cannot be zero! Cannot divide by zero!");
if (val1 < val2)
throw new Exception("Number cannot be less than the divisor!");
else
return (val1 % val2);
} ऊपर हमने दो शर्तों के लिए जाँच की है यानी
- यदि दूसरी संख्या शून्य है, तो एक अपवाद होता है।
- यदि पहली संख्या दूसरी संख्या से कम है, तो एक अपवाद होता है।
शेष दो संख्याओं को वापस करने के लिए, निम्नलिखित पूरा कोड है।
उदाहरण
using System;
namespace Program {
class Demo {
public int RemainderFunc(int val1, int val2) {
if (val2 == 0)
throw new Exception("Second number cannot be zero! Cannot divide by zero!");
if (val1 < val2)
throw new Exception("Number cannot be less than the divisor!");
else
return (val1 % val2);
}
static void Main(string[] args) {
int one = 250;
int two = 200;
int remainder;
Console.WriteLine("Number One: "+one);
Console.WriteLine("Number Two: "+two);
Demo d = new Demo();
remainder = d.RemainderFunc(one, two);
Console.WriteLine("Remainder: {0}", remainder );
Console.ReadLine();
}
}
} आउटपुट
Number One: 250 Number Two: 200 Remainder: 50