We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,390 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Creating a program to split numbers into there separate digits

Hi, I'm having trouble with creating a program to split a number into there own separate digits and then multiplying the digits to create another number. The program will repeat itself until there is just one digit.. The main thing I'm having problem with is figuring out how I could spit the numbers apart..

Can you please help me?

Please and Thank you. -Jenn ^.^

6
Contributors
9
Replies
3 Years
Discussion Span
5 Months Ago
Last Updated
17
Views
kittycat07us
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi, I'm having trouble with creating a program to split a number into there own separate digits and then multiplying the digits to create another number. The program will repeat itself until there is just one digit.. The main thing I'm having problem with is figuring out how I could spit the numbers apart..

Can you please help me?

Please and Thank you. -Jenn ^.^

Create a while loop and inside that loop, extract the right-most digit using the % operator. Then to knock off that digit from the original number, use the divide operator (/).

int number = 12345;
int digit = number % 10;
number = number / 10;

number now holds 1234, digit now holds 5.

VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18

thank you..

kittycat07us
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Now I'm having trouble with another portion of this program. After I split up the digit from the number, all of the digits are suppose to multiply together to create another number. Then the program will repeat itself until it reaches one character. can you help me please?

here is what I have written so far..

int main ()
{
        int num;
        int counter;
        int number;
        int digit[counter];
        counter=0;
        cin>>num;
        cout<<num;
        
       
        while(counter!=sizeof(num))
        {
                while (sizeof(num)!=1)
                {
                	while (num!=0)	
                	{	
                	       digit[counter]=num%10;
                	       number=num/10;
                	       num++;
               		}
                        digit*=digit[counter];
                        num++;
                        
                }
                cout<<" -> "<<digit;
                counter++;
        }
        cout<<" persistence = "<<counter<<endl;        
        return EXIT_SUCCESS;
}
kittycat07us
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

the sizeof() operator returns the number of bytes of memory an object occupies, not the number of digits in a number. So use one loop to fill the array with digits counting as you go, and then another loop to do the multiplication, or use a running subtotal to keep track of the products of the digits as they are encountered.

Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 9

Thank you!!!

kittycat07us
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi again.. This program is still being a butt hole.. I have it set up to take the number, count how many digits are in the number, separating the number into digits, and then multiply the digits together.. And it still doesn't work.. Could you possible look at it please.. I would most appreciate it.. :)

#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cctype>
using namespace std;
int main ()
{
        int num;
        int counter;
        int number;
        int i=0;
        int digit[i];
        int totalDigits=0;
        counter=0;
        //Insert Number
        cin>>num;
        cout<<num;
        while (num>0)

        {
                //counting the digits
                num=num/10;
                totalDigits=counter;
                num++;
                i=counter-1;       
                while (counter>0)
                {
                        //seperate the digits
                        digit[i]=num%10;
                        number=num/10;
                        counter++;
                        while(i!=counter)
                        {
                                //Multiply the digits together
                                digit[i]*=1;
                                i++;
                        }
                //print
                cout<<num<<" -> "<<digit[i];
                }
       }
       cout<<" persistence = "<<counter<<endl;  

       return EXIT_SUCCESS;
       }
kittycat07us
Newbie Poster
16 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi again.. This program is still being a butt hole.. I have it set up to take the number, count how many digits are in the number, separating the number into digits, and then multiply the digits together.. And it still doesn't work.. Could you possible look at it please.. I would most appreciate it.. :)

#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cctype>
using namespace std;
int main ()
{
        int num;
        int counter;
        int number;
        int i=0;
        int digit[i];
        int totalDigits=0;
        counter=0;
        //Insert Number
        cin>>num;
        cout<<num;
        while (num>0)

        {
                //counting the digits
                num=num/10;
                totalDigits=counter;
                num++;
                i=counter-1;       
                while (counter>0)
                {
                        //seperate the digits
                        digit[i]=num%10;
                        number=num/10;
                        counter++;
                        while(i!=counter)
                        {
                                //Multiply the digits together
                                digit[i]*=1;
                                i++;
                        }
                //print
                cout<<num<<" -> "<<digit[i];
                }
       }
       cout<<" persistence = "<<counter<<endl;  

       return EXIT_SUCCESS;
       }

Line 21 -problem. i equals 0, so you are reserving space for an array with 0 elements. How many digits is the longest possible number? Make your array that big or bigger.

const int MAX_LENGTH_NUMBER = 64;
int digit[MAX_LENGTH_NUMBER];

Line 35 - Will you ever get into this loop? counter starts at 0. Does it ever increase to get above 0 BEFORE entering the loop?

Lines 37 - 48 - Once inside the loop, can you ever get out of the loop? If counter is above 0, will it ever again become 0 or less?

Line 44 - What are you trying to do here?

You need to set up a variable called digitProduct and initialize it to 1. The multiply it by every digit that you extract. A digit's value should never change, as far as I can tell.

VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18

This type of problem is best solved with the help of recursion
Here is the algorithm
accept [number]
if number is greater than 9
extract numbers and multiply them together till the original [number] becomes zero
send the result as parameter to this function
if number is not greater than 9
return the answer

As simple as that. I assure you this algorithm is 100% working

xavier666
Junior Poster
173 posts since Sep 2009
Reputation Points: 71
Solved Threads: 10
Skill Endorsements: 1
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  int nro;
  int nroCifras=0;
  int temp;

  cout << "Insert number: ";
  cin >> nro;

  temp = nro;

  if(nro>0)
  {
      while(temp>0)
      {
          temp = temp/10;
          nroCifras++;
      }

      //Display 1 2 3 4 5
      for(int i=nroCifras-1;i>=0;i--)
      {
          cout << nro/int(pow(10,i)) << " ";
          nro = nro%int(pow(10,i));

      }
  }

  return 0;
}
SantiagoQC
Newbie Poster
1 post since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0839 seconds using 2.72MB