20_10

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

 Source code:

//Write a program in C to count the number of digits of a given number using recursion. 


#include<stdio.h>

int noOfDigits(int n)

{

    static int digit=0;


    if(n!=0)

    {

        digit++;

        noOfDigits(n/10);

    }


    return digit;

}

int main()

{

  int n,digit;

    printf("Enter a number: ");

    scanf("%d",&n); /*Enter Number of digits */


    digit = noOfDigits(n);


    printf("The number of digits in the number is :  %d\n",digit);

    return 0;

}

Output:
count the number of digits of a given number using recursion


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...