+92 332 4229 857 99ProjectIdeas@Gmail.com

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


Split number in forward direction
split_nf() splits the number into digits in the forward 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_nf(int num)
{
int x=0,y=0;
int a=0,b=0;
int count=0;
char tempStr[11];

       itoa ( num , tempStr , 10 );
       b = strlen(tempStr);

       while(count!=strlen(tempStr))
       {
              b=b-1;
              a=static_cast<int>(pow(10.0,b));
              x=num/a;
              y=num%a;
              num=y;
              count++;

              cout<<setw(3)<<x;
       }
}

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

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

0 comments: