C# Sharp Exercises: Display the individual digits of a given number
C# Sharp Recursion: Exercise-4 with Solution
Write a program in C# Sharp to display the individual digits of a given number using recursion.
Pictorial Presentation:
Sample Solution:
C# Sharp Code:
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);
}
}
Sample Output:
Recursion : Display the individual digits of a given number : ------------------------------------------------------------------ Input any number : 25 The digits in the number 25 are : 2 5
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a program in C# Sharp to find the sum of first n natural numbers using recursion.
Next: Write a program in C# Sharp to count the number of digits in a number using recursion.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework