﻿ C# Sharp Exercises: Count the number of digits in a number - w3resource

# C# Sharp Exercises: Count the number of digits in a number

## C# Sharp Recursion: Exercise-5 with Solution

Write a program in C# Sharp to count the number of digits in a number using recursion.

Sample Solution:-

C# Sharp Code:

``````using System;

class RecExercise5
{
static void Main(string[] args)
{

Console.Write("\n\n Recursion : Count the number of digits in a number :\n");
Console.Write("---------------------------------------------------------\n");
Console.Write(" Input any number : ");
Console.Write("\n The number {0} contains number of digits : {1} ",num,getDigits(num, 0));
}

public static int getDigits(int n1, int nodigits)
{
if (n1 == 0)
return nodigits;

return getDigits(n1 / 10, ++nodigits);
}
}
```
```

Sample Output:

``` Recursion : Count the number of digits in a number :
---------------------------------------------------------
Input any number : 1000

The number 1000 contains number of digits : 4
```

C# Sharp Code Editor:

```using System;

class RecExercise5
{
static void Main(string[] args)
{

Console.Write("\n\n Recursion : Count the number of digits in a number :\n");
Console.Write("---------------------------------------------------------\n");
Console.Write(" Input any number : ");
Console.Write("\n The number {0} contains number of digits : {1} ",num,getDigits(num, 0));