Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- digits_of_number.c
- Extracting digits of number.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- https://www.facebook.com/dmilicev
- */
- #include <stdio.h>
- int main(void)
- {
- int i, n = 1234;
- printf("\n n = %d \n", n );
- printf("\n first digit (thousands) n/1000 %% 10 is %d \n", n/1000%10 );
- printf("\n second digit (hundreds) n/100 %% 10 is %d \n", n/100%10 );
- printf("\n third digit (tens) n/10 %% 10 is %d \n", n/10%10 );
- printf("\n fourth digit (ones) n/1 %% 10 is %d \n", n%10 );
- printf("\n while loop: \n");
- while( n != 0 )
- {
- printf("\n n = %4d last digit is %d \n", n, n%10 );
- n = n / 10;
- }
- printf("\n After while loop n = %d \n", n );
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment