How to call custom methods in C#?

CsharpProgrammingServer Side Programming

To define a custom method in C#, use the following syntax −

<Access Specifier> <Return Type> <Method Name>(Parameter List) {
   Method Body
}

To call a custom method, try to run the following code. It has the checkPalindrome() method which is called to checker whether the binary representation is Palindrome or not −

Example

 Live Demo

using System;

public class Demo {
   public static long funcReverse(long num) {
      long myRev = 0;

      while (num > 0) {
         myRev <<= 1;

         if ((num & 1) == 1)
         myRev ^= 1;
         num >>= 1;
      }
      return myRev;
   }
   public static bool checkPalindrome(long num) {
      long myRev = funcReverse(num);
      return (num == myRev);
   }
   public static void Main() {
      // Binary value of 5 us 101
      long num = 5;

      if (checkPalindrome(num))
      Console.WriteLine("Palindrome Number");
      else
      Console.WriteLine("Not a Palindrome Number");
   }
}

Output

Palindrome Number
raja
Published on 09-Aug-2018 08:29:02
Advertisements