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 ^.^

Recommended Answers

All 10 Replies

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.

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;
}

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.

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;
       }

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.

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

#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;
}

Q.4: Develop a C++ program to perform the following task as mentioned below via example:
Step No:1: Enter Any Number from User: 93 ]
Step No:2: Split the Input Number like this = 9 and 3
Step No:3: Add the number like this = 9 + 3 = 12
Step No.4: Again Spilt the Number and then Add and display result like this = 1+2 = 3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.