Recursive Function in C# and its Example
A function which calls itself or is in a potential cycle of function calls
For Example:
Scenario: An Evens number is an integer whose digits are all even. For example 2426 is an Evens number but 3224 is not.
Write a function named isEvens that returns 1 if its integer argument is an Evens number otherwise it returns 0.
The function signature is int isEvens (int n).
Solution:
using System;
namespace evenNumber
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Integer Value");
string input = Console.ReadLine();
int Evens = Convert.ToInt32(input);
int a = isEvens(Evens);
Console.WriteLine("value return: " + a);
Console.ReadLine();
}
private static int isEvens(int n)
{
int a = n % 10;
n = n / 10;
if (a == 0 || a == 2 || a == 4 || a == 6 || a == 8)
{
if (n == 0)
{
return 1;
}
else
{
return isEvens(n);
}
}
else
{
return 0;
}
}
}
}
In above example, isEvens(n) function called itself.
Output: