To write a C# program to reverse a number using recursive function.
Import the 'System' namespace to use the classes present in the 'System' namespace.
Declare a function to recursive reverse the digits in the number .
In the recursive function , find the remainder of the digit.If the remainder is equal to 0 then return 0 else add the remainder obtained and then call the function 'fun' again untill it turns 0.
Declare the Main function and get the user input .
Call the function and execute the program.
using System;
namespace Program
{
class Program
{
int rem = 0, rev = 0;
public int fun(int number)
{
rem = number % 10;
if (rem == 0)
{
return rev;
}
else
{
rev = rev * 10 + rem;
return fun(number / 10);
}
}
static void Main(string[] args)
{
int n;
Console.WriteLine("Enter a Number: ");
n = Convert.ToInt32(Console.ReadLine());
Program reverse= new Program();
Console.WriteLine("Reverse Number : {0} ",reverse.fun(n));
}
}
}
A C# program to reverse a number using recursive function is written and executed.