+92 332 4229 857 99ProjectIdeas@Gmail.com

Split number into digits in backward direction (C++)


Split number in backward direction
split_nb() splits the number into digits in the backward direction and it takes 1 parameter:
num takes the value which you want to split into digits.
num takes the value between 1 to 10 digits because its data type is integer.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"
#include "math.h"

using namespace std;

void split_nb(int num)
{
  int x=0,y=0,count=0;
  char tempStr[11];

       itoa ( num , tempStr , 10 );

       while(count!=strlen(tempStr))
       {
              x=num/10;
              y=num%10;
              num=x;
              count++;
              cout<<setw(3)<<y;
       }
}

int main()
{
       split_nb ( 1 );cout<<endl;
       split_nb ( 12 );cout<<endl;
       split_nb ( 123 );cout<<endl;
       split_nb ( 1234 );cout<<endl;
       split_nb ( 12345 );cout<<endl;
       split_nb ( 123456 );cout<<endl;
       split_nb ( 1234567 );cout<<endl;
       split_nb ( 12345678 );cout<<endl;
       split_nb ( 123456789 );cout<<endl;
       split_nb ( 1234567890 );cout<<endl;
      
       _getche();
       return 0;
}

Output
  1
  2  1
  3  2  1
  4  3  2  1
  5  4  3  2  1
  6  5  4  3  2  1
  7  6  5  4  3  2  1
  8  7  6  5  4  3  2  1
  9  8  7  6  5  4  3  2  1
  0  9  8  7  6  5  4  3  2  1

0 comments: