Advertisement
dmilicev

number_of_digits_without_using_loop_v1.c

Jun 5th, 2020
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     number_of_digits_without_using_loop_v1.c
  4.  
  5.     https://en.wikipedia.org/wiki/Logarithm
  6.     https://www.mathsisfun.com/algebra/logarithms.html
  7.  
  8.     log(10)    = 1
  9.     log(100)   = 2
  10.     log(1000)  = 3
  11.     log(10000) = 4
  12.     . . .
  13.  
  14.  
  15.     You can find all my C programs at Dragan Milicev's pastebin:
  16.  
  17.     https://pastebin.com/u/dmilicev
  18.  
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <math.h>       // for log10()
  23.  
  24. int main(void)
  25. {
  26.     long long num = 999999991234567890;
  27.     int count = 0;
  28.  
  29.     //count = ( num == 0 ) ? 1 : ( log10(num)+1 );      // or
  30.  
  31.     if( num == 0 )
  32.         count = 1;
  33.     else
  34.         count = log10(num)+1;
  35.  
  36.     printf("\n In number %lld there are total digits %d \n", num, count);
  37.  
  38.     printf("\n In number %llu there are total digits %d \n", num, count);
  39.  
  40.  
  41.     return 0;
  42.  
  43. } // main()
Advertisement
RAW Paste Data Copied
Advertisement