Advertisement
dmilicev

digits_of_number.c

Dec 26th, 2019
270
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None
  1. /*
  2.  
  3.     digits_of_number.c
  4.  
  5.     Extracting digits of number.
  6.  
  7.  
  8.     You can find all my C programs at Dragan Milicev's pastebin:
  9.  
  10.     https://pastebin.com/u/dmilicev
  11.  
  12.     https://www.facebook.com/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. int main(void)
  19. {
  20.     int i, n = 1234;
  21.  
  22.     printf("\n n = %d \n", n );
  23.  
  24.     printf("\n first  digit (thousands)   n/1000 %% 10  is  %d \n", n/1000%10 );
  25.  
  26.     printf("\n second digit (hundreds)    n/100 %% 10   is  %d \n", n/100%10 );
  27.  
  28.     printf("\n third  digit (tens)        n/10 %% 10    is  %d \n", n/10%10 );
  29.  
  30.     printf("\n fourth digit (ones)        n/1 %% 10     is  %d \n", n%10 );
  31.  
  32.  
  33.     printf("\n while loop: \n");
  34.  
  35.     while( n != 0 )
  36.     {
  37.         printf("\n n = %4d  last digit is  %d \n", n, n%10 );
  38.         n = n / 10;
  39.     }
  40.  
  41.     printf("\n After while loop  n = %d \n", n );
  42.  
  43.  
  44.     return 0;
  45.  
  46. } // main()
Advertisement
RAW Paste Data Copied
Advertisement