Q:

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

0

Write a program in C# Sharp to count the number of digits in a number using recursion. 
Test Data :
Input any number : 12345
Expected Output :
The number 12345 contains number of digits : 5

All Answers

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

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 : ");
        int num = Convert.ToInt32(Console.ReadLine());           
        Console.Write("\n The number {0} contains number of digits : {1} ",num,getDigits(num, 0));
        Console.ReadLine();
        }
public static int getDigits(int n1, int nodigits)
    {
    if (n1 == 0)
        return nodigits;

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

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