To write a C# program to reverse a number using recursive function.
Create a method (ReverseNumber) that takes an integer as input and returns its reversed form.
Check if the number is 0. If so, return the reversed number.
Use modulo operator (%) to get the last digit of the number.
Multiply the current reversed number by 10 and add the last digit.
Call the method with the remaining digits by dividing the number by 10.
DEVELOPED BY : NIROSHA S
REGISTER NO: 212222230097
using System;
namespace program11{
class Program
{
static int ReverseNumber(int num, int reversedNum = 0)
{
if (num == 0)
return reversedNum;
int lastDigit = num % 10;
reversedNum = reversedNum * 10 + lastDigit;
return ReverseNumber(num / 10, reversedNum);
}
static void Main(string[] args)
{
Console.Write("Enter a number to reverse: ");
int number = Convert.ToInt32(Console.ReadLine());
int reversedNumber = ReverseNumber(number);
Console.WriteLine("Reversed Number: " + reversedNumber);
}
}
}
The program for reverse a number using recursion was executed successfully.