Separating Digits Program

closed account (oG8qGNh0)
There are 2 parts to Separating Digits;

#1. Write a program that takes a non-negative long or int as input and displays each of the digits on a separate line. Note: you may not assume that the number entered will be any particular number of digits. The program output should look similar to:

Enter an integer: 7134

4
3
1
7

Hint: Here’s a few questions to guide you:
What value would you divide ANY number by to get rid of its right-most digit?
Once you have the answer above….know that performing a modulus operation by that same amount will isolate the right-most digit for you (since it is the remainder)
If you kept repeating that process, when would you know to stop?

My Program:

//Seperate from right to left
#include <iostream>
// C++ input/output library
using namespace std;
// std namespace functions being used
int main() {
cout<<"Enter an integer (Four digits): ";
// prompt the user to enter a number
char input[4];
// create a new array of characters
cin>>input;
// get four numbers into input
cout<<input[3]<<endl<<input[2]<<endl<<input[1]<<endl<<input[0];
// output each character on a different line backwards
fflush(stdin);
// flush stdin
cin.get();
// wait for user to press etner, so they see the output
return 0;
// exit program
}

#2. Write a program that takes a non-negative long or int as input and displays each of the digits on a separate line. Note: you may not assume that the number entered will be any particular number of digits. The program output should look similar to:

Enter an integer: 7134
7
1
3
4


Hint: You will need more than one loop to solve this problem:

(1) one to figure out how many digits there are (how can you remove a digit one at a time until there are none?) and

(2) one to slowly display the digits from left to right. Keep in mind that if you reduce the number during the loop that figures out the number of digits, you will need a copy of that original number for the second step.

My Program:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int number;
cout << "Enter a number to separate it's digits: ";
cin >> number;
int exponent = floor( log10( static_cast<double>(number) ) );

int divisor;
while (number != 0)
{
divisor = pow(10.0, exponent);
cout << number/divisor << " ";
number %= divisor;
exponent--;
}
cout << endl;

system("pause");
return 0;
}



Are there anything wrong with my codes?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

void writeOne( int n, bool up )
{
   if ( n < 10 )
   {
      cout << n << '\n';
   }
   else
   {
      if (  up ) cout << n % 10 << '\n';
      writeOne( n / 10, up );
      if ( !up ) cout << n % 10 << '\n';
   }
}


int main()
{
   int N;
   cout << "Input a number: ";   cin >> N;
   cout << "Up:  \n";   writeOne( N, true  );
   cout << "Down:\n";   writeOne( N, false );
}


Input a number: 7134
Up:  
4
3
1
7
Down:
7
1
3
4

Another alternative would be to convert the entered number to a string and then display. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

void writeOne(unsigned int n, bool up)
{
	if (const auto str = std::to_string(n); up)
		for (auto ich = str.crbegin(); ich != str.crend(); ++ich)
			std::cout << *ich << '\n';
	else
		for (const auto& ch : str)
			std::cout << ch << '\n';
}


int main()
{
	unsigned int N;

	std::cout << "Input a number: ";
	std::cin >> N;

	std::cout << "Up:  \n";
	writeOne(N, true);

	std::cout << "Down:\n";
	writeOne(N, false);
}

.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;


int main()
{
   ostream_iterator<char> it{ cout, "\n" };
   string N;
   cout << "Input a number: ";   cin >> N;
   cout << "Up:  \n";   copy( N.rbegin(), N.rend(), it );
   cout << "Down:\n";   copy( N.begin() , N.end() , it );
}
closed account (oG8qGNh0)
How is your background color blue and mine shows nothing? Where did you copy and paste the code from?
this is code formatting tags, the <> on the side editor.
please use them, it does syntax and formatting that is cleaner than just plain text of typing junk in here without.
closed account (oG8qGNh0)
}
Last edited on
closed account (oG8qGNh0)
]
Last edited on
@naveenmmenon
How is your background color blue and mine shows nothing? Where did you copy and paste the code from?


@jonnin
this is code formatting tags, the <> on the side editor.
please use them, it does syntax and formatting that is cleaner than just plain text of typing junk in here without.


@naveenmmenon
aight bet thanks

@naveenmmenon
posts code again without code tags.

Pleas click the Format button that looks like "<>" and post your code between the tags.

I the initial post the button does not work. Simply type "[ code]" and "[ /code]" (without the spaces), and paste your code between the tags. It will make it easier to review your code.

By the way, you can edit your previous post. Highlight the code section and then click the "<>" button. That will insert the tags around the highlighted code.
closed account (oG8qGNh0)
1st Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Seperate from right to left
#include <iostream> 
// C++ input/output library
using namespace std; 
// std namespace functions being used
int main() {
cout<<"Enter an integer (Four digits): "; 
// prompt the user to enter a number
char input[4]; 
// create a new array of characters
cin>>input; 
// get four numbers into input
cout<<input[3]<<endl<<input[2]<<endl<<input[1]<<endl<<input[0]; 
// output each character on a different line backwards
fflush(stdin); 
// flush stdin
cin.get(); 
// wait for user to press etner, so they see the output
return 0; 
// exit program
}


2nd Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;

int exponent = floor( log10( static_cast<double>(number)) );
int divisor;
while (number != 0)
{
divisor = pow(10.0, exponent);
cout << number/divisor << " ";
number %= divisor;
exponent--;
}

cout << endl;

system("pause");
return 0;
}


My teacher commented:

Naveen - you must do this while still treating the variable as numeric - not just an array of chars - also should work for any length - please try again. She gave me a 0. Any chance you know how to fix this situation?

The two programs should be SEPERATE not COMBINED! How do you fix this mathematically?
Last edited on
you must do this while still treating the variable as numeric


That wasn't what you originally stated!

also should work for any length


No. There is no such thing in C++ as a numeric variable with unlimited length. You have
unsigned int
unsigned long int
unsigned long long int

Your original post referred to unsigned long.

laastchance's post showed how to do this treating as numeric without conversion. Having two programs is stupid. What you need is two functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

void up(unsigned long n)
{
	if (n < 10)
		std::cout << n << '\n';
	else {
		std::cout << n % 10 << '\n';
		up(n / 10);
	}
}

void down(unsigned long n)
{
	if (n < 10)
		std::cout << n << '\n';
	else {
		down(n / 10);
		std::cout << n % 10 << '\n';
	}
}


int main()
{
	unsigned long N;

	std::cout << "Input a number: ";
	std::cin >> N;

	std::cout << "Up:  \n";   up(N);
	std::cout << std::endl;
	std::cout << "Down:\n";   down(N);
}


If teacher really, really insists on having two programs then it's easy enough to split.

Last edited on
closed account (oG8qGNh0)
This is the rubric!

Both solutions commented to explain math involved
2.0 pts - sufficient explanation

both problems - type of input is long or int
1.0 pts - Full Marks

prob 1: division + modulus used to isolate digits and cut numbers
2.0 pts - Full Marks

prob 1: loop until complete - accommodates any legal value
2.0 pts - Full Marks

prob 1: output 1-digit per line in correct order
2.0 pts - Full Marks

prob 2: determine number of digits before separating
2.0 pts - Full Marks

prob 2: division + modulus used to isolate digits and cut numbers
2.0 pts - Full Marks

prob 2: loop until complete - accommodates any legal value
2.0 pts - Full Marks

prob 2: output 1-digit per line in correct order
2.0 pts - Full Marks
Great. I wondered if it was going to be rejected as it used recursion.

For info, the non-recursive loop way is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>

void up(unsigned long n)
{
	for (; n > 0; n /= 10)
		std::cout << n % 10 << '\n';
}

void down(unsigned long n)
{
	for (unsigned long div = std::pow(10, std::floor(std::log10(n))); n > 0; n %= div, div /= 10)
		std::cout << n / div << std::endl;
}

int main()
{
	unsigned long N;

	std::cout << "Input a number: ";
	std::cin >> N;

	std::cout << "Up:  \n";   up(N);
	std::cout << std::endl;
	std::cout << "Down:\n";   down(N);
}

Last edited on
Topic archived. No new replies allowed.