Split number into digits

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yasirassassin
    New Member
    • Nov 2011
    • 7

    Split number into digits

    hey sir i am having problem in splitting a number into its digits if i use remainder and division operation then i can split a number and print it in reverse order but dont know how to split that number and print that in correct order
    here is my program

    #include<iostre am>
    using namespace std;
    int main()
    {
    int x=12345,r,c=1;
    while(c<=5){
    r=x%10;
    x=x/10;
    cout<<r;
    c++;


    }

    cin.get();
    return 0;
    }
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    Follow this algorithm:

    (12345/10000)%10 = 1
    (12345/1000)%10 = 2
    (12345/100)%10 = 3
    (12345/10)%10 = 4
    (12345/1)%10 = 5


    Implement this algorithm in code and off you go!

    EDIT: Obviously the "%10" in line 1 and the "/1" in line 5 is not needed. But I gave it this way to make the algorithm more obvious, in case you implement a loop.

    Comment

    • yasirassassin
      New Member
      • Nov 2011
      • 7

      #3
      well mmm i did this before i read your reply i think i succeeded anyway thanks for reply i'm really grateful to you

      #include<iostre am>
      using namespace std;
      int main()
      {
      int x=12345,c=1,n=1 0000,fin;
      while(c<=5){
      fin=x/n;
      n=n/10;

      c++;
      cout<<fin<<endl ;
      }
      cout<<endl;
      cout<<"at the end here you go "<<fin<<end l;

      cin.get();
      return 0;
      }

      Comment

      • alexis4
        New Member
        • Dec 2009
        • 113

        #4
        while(c<=5){
        fin=x/n;
        n=n/10;

        c++;
        cout<<fin<<endl ;
        }
        How can that work? You are printing fin right? So:

        Loop 1: fin=12345/10000 = 1
        Loop 2: fin = 12345/1000 = 12

        So how could you separate the digit without the remainder?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          This is C++. You might try:

          Code:
          stringstream s;
           s << 12345;
           string str;
           s >> str;

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Does your function need to handle negative inputs?

            Look at your code in the original post. Looping 5 times only works if the input is a 5-digit number. Instead, loop until x/10 is zero.

            Your original algorithm collects digits from right to left. Make the change I suggested above and your original algorithm works for all positive inputs regardless of how many digits they have. On the other hand, the revised algorithms that collect digits from left to right are tuned to a specific number of digits. I suggest you use the original algorithm to fill an array with digits and then print the array contents in reverse order. That leaves you with the most flexible function.

            Comment

            • yasirassassin
              New Member
              • Nov 2011
              • 7

              #7
              ok how about this one check this out
              #include<iostre am>
              using namespace std;
              int main()
              {
              int x=12345,c=1,n=1 0000,fin;
              while(c<=5){
              fin=x/n;
              n=n/10;
              fin%=10;
              c++;
              cout<<fin<<endl ;
              }
              cout<<endl;
              cin.get();
              return 0;
              }

              Comment

              • alexis4
                New Member
                • Dec 2009
                • 113

                #8
                It seems correct. Does it work OK?

                Comment

                • nehatrivedi
                  New Member
                  • Nov 2011
                  • 1

                  #9
                  #include <iostream>
                  #include <cstdlib>
                  #include <math.h>
                  using namespace std;
                  int main()
                  {
                  int c=0,fin,r;
                  int x,xx,n;
                  cout<<"Enter the number ; ";
                  cin>>x;
                  xx=x;
                  while(x!=0)
                  {
                  r=x%10;
                  c++;
                  x=x/10;
                  }
                  c--;
                  n=pow(10,c);
                  while(n!=0)
                  {
                  fin=xx/n;
                  fin=fin%10;
                  cout<<fin<<endl ;
                  n=n/10;
                  }
                  cout<<endl;
                  system("PAUSE") ;
                  return EXIT_SUCCESS;
                  }

                  Comment

                  • shilpa george
                    New Member
                    • Oct 2011
                    • 15

                    #10
                    while(c<=5){
                    fin=x/n; //************** fin=(int)x/n;
                    n=n/10;

                    c++;
                    cout<<fin<<endl ;
                    }

                    Comment

                    Working...