Advertisement
dmilicev

number_of_digits_without_using_loop_v2.c

Jun 5th, 2020
159
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     number_of_digits_without_using_loop_v2.c
  4.  
  5.     Version with recursion from Bishal Raj Bhattarai
  6.     https://web.facebook.com/profile.php?id=100009001371943
  7.  
  8.  
  9.     You can find all my C programs at Dragan Milicev's pastebin:
  10.  
  11.     https://pastebin.com/u/dmilicev
  12.  
  13. */
  14.  
  15. #include <stdio.h>
  16.  
  17. int count_digits(long long x)
  18. {
  19.     static int counter=0;   // must be static because of recursion
  20.  
  21.     if( x > 0 )
  22.     {
  23.         count_digits(x/10);
  24.         counter++;
  25.     }
  26.  
  27.     return counter;
  28. }
  29.  
  30. int main(void)
  31. {
  32.     long long num = 999999991234567890;
  33.     int count;
  34.  
  35.     count = count_digits(num);
  36.  
  37.     printf("\n In number %lld there are total digits %d \n", num, count);
  38.  
  39.     printf("\n In number %llu there are total digits %d \n", num, count);
  40.  
  41.  
  42.     return 0;
  43.  
  44. } // main()
Advertisement
RAW Paste Data Copied
Advertisement