Q:

Write a program in C# Sharp to display the individual digits of a given number using recursion

1

Write a program in C# Sharp to display the individual digits of a given number using recursion. 
Test Data :
Input any number : 1234
Expected Output :
The digits in the number 1234 are : 1 2 3 4

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
public class RecExercise4
{
    static void Main()
    {
    Console.Write("\n\n Recursion : Display the individual digits of a given number :\n");
    Console.Write("------------------------------------------------------------------\n");
    Console.Write(" Input any number : ");
    int num = Convert.ToInt32(Console.ReadLine()); 
    Console.Write(" The digits in the number {0} are : ",num);
    separateDigits(num);
    Console.Write("\n\n");
    }
    static void separateDigits(int n)
    {
        if (n < 10)
        {
            Console.Write("{0}  ", n);
            return;
        }
        separateDigits(n / 10);
        Console.Write(" {0} ", n % 10);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now