Digit Extraction

Can someone help me with digit extraction. You have to define a variable to store user input, including appropriate comments. Prompt the user for a number and use cin to place the value in the variable you defined in the preceding step and then display each digit seperately.
If you want to seperate the digits of a number use modulo eg.
1
2
3
cin >> number;
 digit=number%10;
 cout << digit;
Hi louisa; I did this and its giving me the numbers but I need it to print tens, thousands, like that but it is not doing this, this is my code.

#include <iostream> // required to perform C++ stream I/O

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution

int main () {

int num ;
int rem ;

cout << " Input a five-digit number : ";
cin >> num;;

for (int i = 0; i < 5; i++) {
rem = num %10;
num = num / 10;
cout << rem << endl;
}

return 0; // indicate that program ended successfully

} // end function main

so you want it like this: 254 becomes 4, 50, 200?
Screen should output this:

Enter Five Digit number: 12345
Ones digit is: 5
Tens digit is: 4
Hundreds digit is: 3
Thousands digit is: 2
Ten-thousands digit is: 1
Press any key to continue
unless another forum member has a better solution you will have to use if. I would suggest using your variable i and the using something like this:
1
2
3
4
5
6
7
8
 if (i==1)
{
     cout << "Ones digit is: " << digit;
}
if (i==2)
{
    cout << "Tens digit is: " << digit;
} 


and so on. does that make sense?
This implementation will allow you to enter a variable number of digits, I've shown you the code to get 5 digits working, you can expand on the code to make it variable by playing around with the number of digits entered variable....:

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
#include <iostream>
#include <string>
#include <vector>
#include <conio.h> //for _getch(); 
#include <sstream> 
using namespace std;

int main() {
	string number_string; 
	int no_digits; 

	cout << "Please enter a number: "; 
	cin >> number_string; 

	//number of digits = the length of the string entered
	no_digits = number_string.length(); 

	if (no_digits == 5) { //for instance the length is 5 as in your example
		cout << "The ones digit is: " << number_string[4] << endl; 
		cout << "The tens digit is: " << number_string[3] << endl; 
		cout << "The hundreds digit is: " << number_string[2] << endl; 
		cout << "The thousands digit is: " << number_string[1] << endl; 
		cout << "The ten-thousands digit is: " << number_string[0] << endl; 
	}

	cout << "\nPress any key to continue..."; //number of digits = the length of the string entered
	_getch(); 
	return 0; 
}


Hint use stringstream to type cast string -> integer. It's in the tutorial on this site if you don't know what it is yet. .
Last edited on
Louisa pretty much gave you what you need to know...

You can extract all numbers (as long as you know their length) with modeulus operator and division together..

so ex. "2534"

2534/1000 = 2.

2534%1000 = 534;

534/100 = 5;

534%100 = 34;

34/10 = 3;

34%10 = 4

4/1 = 4;



this works with any number ofcourse just change the division and mod from 1000 to whatever you are working with. You mentioned 5 digits so just move it to 10000. and work down from there.

EDIT: The division works as long as its all in int btw because that does the favor of discarding the remainder
Last edited on
Hi vlad61; This is an intro to programming and the code has to be written like a beginner, can you take a look at my code and tell me if I have it correctly. It does work but it doesn't display the words

Screen should output this:

Enter Five Digit number: 12345
Ones digit is: 5
Tens digit is: 4
Hundreds digit is: 3
Thousands digit is: 2
Ten-thousands digit is: 1
Press any key to continue

My code:

#include <iostream> // required to perform C++ stream I/O

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution

int main () {

int num ;
int rem ;

cout << " Input a five-digit number : ";
cin >> num;;

for (int i = 0; i < 5; i++) {
rem = num %10;
num = num / 10;
cout << rem << endl;
}

return 0; // indicate that program ended successfully

} // end function main

This is the code I have so far.

int main()
{
int fnum1;

cout <<"\nEnter 5 Digit Number:";
cout << endl;
cin >> fnum1;
fnum1 = (12345 % 10);
cout <<"\nOnes Digit is:" << fnum1;
cout <<"\nTens Digit is:";


return 0; // indicate that program ended successfully

}



I can get it to display the ones digit, but i am not sure how to get it to display the rest... any help?
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
35
36
37
38
39
40
#include <iostream> // required to perform C++ stream I/O
#include <string>

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution

int main () {

int num, rem;

string decimal_place = "TEST";

cout << " Input a five-digit number : ";
cin >> num;
cin.ignore();

for (int i = 0; i < 5; i++) 
{
	rem = num %10;
	num = num /10;

	if(i == 0)
		decimal_place = "Ones";
	else if(i == 1)
		decimal_place = "Tens";
	else if(i == 2)
		decimal_place = "Hundreds";
	else if(i == 3)
		decimal_place = "Thousands";
	else if(i == 4)
		decimal_place = "Tens Thounsand";

	cout<<decimal_place<<" digit is: "<< rem << endl;
}

cin.ignore();
return 0; // indicate that program ended successfully

} // end function main 



Why not just put an if statement? I mean its not as clean i guess but it works fine for this

BTW: I like your little loop for seperating numbers its much more simplified than mine :)
Last edited on
What about the way I did it y does it not work?
Okay, does it actually need to print out words, i.e. "Ones digit is: 5", or can it print numbers like "1's digit is: 5", because that would be much easier for you.

I've changed and/or added to your code at line 2 and lines 19-23.
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
#include <iostream> // required to perform C++ stream I/O
#include <math.h> //used for the pow function (and other math things like sin, arctan, and log)

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution

int main () {

	int num ;
	int rem ;

	cout << " Input a five-digit number : ";
	cin >> num;;

	for (int i = 0; i < 5; i++) {
		rem = num %10;
		num = num / 10;
		cout << pow(10, i) << "'s digit is: " << rem << endl;
		/*      ^^^^^^^^^^ This is the number place we are in, and this function is read as 10i 
		 * Note: we are using the for-loop's index i
		 * (This pow function comes from the <math.h> header file)
		 */
	}

	return 0; // indicate that program ended successfully

} // end function main 
Thanks I will try it and see if it works, thanks everyone for all your help.
Topic archived. No new replies allowed.