C language: enter a positive integer with uncertain digits to flip the digits!

Source: Internet
Author: User
Code:
  1. /* Give a positive integer with the following requirements:
  2. 1. Find the number of digits;
  3. 2. output each digit separately;
  4. 3. output the numbers in reverse order,
  5. */
  6. # Include <stdio. h>
  7. # Include <math. h>
  8. Void main ()
  9. {
  10. Int A, B, C, D, I = 0;
  11. Int sum = 0;
  12. Int * P = new int [];
  13. Printf ("enter a number :");
  14. Scanf ("% d", & );
  15. // Determine the number of digits;
  16. Do {
  17. I ++;
  18. P [I] = A % 10;
  19. A = A/10;
  20. } While (! = 0 );
  21. // Output the value in the dynamic array
  22. D = I;
  23. While (D! = 0 ){
  24. Printf ("% d,", P [d]);
  25. D --;
  26. }
  27. Printf ("the number of digits is % d/N", I );
  28. // Print this value using the reverse string. Use the POW function POW (x, y) --> Y of X;
  29. For (C = 1; C <= I; C ++ ){
  30. B = POW (10, C-1 );
  31. Sum = B * P [c] + sum;
  32. Printf ("% d/N", sum );
  33. }
  34. Delete [] P;
  35. }

Here I think of the dynamic array method to save the result of each remainder operation. It seems like a pointer also works. I don't have to remember it for a long time!

The method for attaching a dynamic array is as follows:

Int main ()
{
// Use the pointer P to point to the new dynamically allocated memory space with the length Len * sizeof (INT)
Int * P = new int []; // if it is C ++, the system will warn that no array length is specified.
...........
Delete [] P;
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.