How to calculate the number of digits of an integer & Save the numbers on each one

Source: Internet
Author: User

Use the loop to solve ~ ~

M divided by 10, and then counted with a variable count, plus 1 per loop, until the number is less than 10 after the number, Count plus 1.

Example: Integer m=4325,

First: 4325/10=432,m results are not for 0,cout=1;

First: 432/10=43,m results are not for 0,cout=2;

Third time: 43/10=4,m result is not 0,count=3;

The fourth time: M is 4, the result is not 0,count=4; but M/10 is 0, and the loop is over.

As for each digit that holds an integer, we can use the most conventional method, using modulo (%) and division (/) to obtain each digit ~ ~ ~

You can also take advantage of the above cycle, because each time divided by 10 after the number, then modulo, you can get the number at the end of the digit,

  1. <span style=" FONT-SIZE:18PX; " > #include  " iostream " &NBSP;&NBSP;
  2. using  namespace  std;   
  3. int Main ()
  4. {
  5.      int  n,m=0,j=0;   
  6. int  A[10];
  7. cin>>n;
  8. while (n!=0)
  9. {
  10.          int  x=n%10;   
  11. A[j]=x; //This is to save the number on each   
  12. j + +;
  13. N=N/10; //Each time divided by 10, the result is not 0, then M can add 1.   
  14. m++;
  15. }
  16. cout<<m<<endl; //number of output bits   
  17.       for Span style= "margin:0px; padding:0px; Border:currentcolor ">  (int  i=0;i<m;i++ )   
  18.      {  
  19.          cout<<a[i]<< '   ' ; //flashback output per digit    
  20. }
  21. return  0;
  22. }</span>

How to calculate the number of digits of an integer & Save the numbers on each one

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.