Finding the value of a digit

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 6
Reputation: luvsom is an unknown quantity at this point 
Solved Threads: 0
luvsom luvsom is offline Offline
Newbie Poster

Finding the value of a digit

 
0
  #1
May 6th, 2009
I've been struggling with this for days now......

Say the number entered is 12345

If the user wants to find the value of the 4th digit, which would be the 2....how can I do this???
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Finding the value of a digit

 
0
  #2
May 6th, 2009
Divide (/) by 10
Modulus (%) by 10
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,051
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 311
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: Finding the value of a digit

 
0
  #3
May 6th, 2009
He's counting his digits from right to left Walt.
So IMHO it has to be more something like this : (pseudocode)

number = 12345;
n = 4; //we seek the fourth digit from the right
num = number / 10^(n-1);
digitvalue = num % 10; //this we want
Last edited by ddanbe; May 6th, 2009 at 12:45 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: luvsom is an unknown quantity at this point 
Solved Threads: 0
luvsom luvsom is offline Offline
Newbie Poster

Re: Finding the value of a digit

 
0
  #4
May 6th, 2009
Ok so this is what I have so far. I can't figure out how to get it to work. What am I doing wrong? Oh and it's a she not he

int main()
{
	int num = 0;
	int number = 0;
	int n = 0;
	int result = 0;


	cout << "Enter a number: ";
	cin >> number;
	cout << "Enter a digit position:";
	cin >> n;

	
	if ( 0 == number)
		{
		 cout << "0";
		}

	while (n-- > 1)
	{
	num = number / (10^n-1);
	result = num % 10;
	}


	cout << "\nThe value of this digit is: " << result << endl;
	system("Pause");
	
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the value of a digit

 
0
  #5
May 6th, 2009
>>num = number / (10^n-1);

you have to use pow() function
num number / pow(10,n)-1;
The problem with that is if pow() returns 1 then the formula will get a divide by 0 error.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: luvsom is an unknown quantity at this point 
Solved Threads: 0
luvsom luvsom is offline Offline
Newbie Poster

Re: Finding the value of a digit

 
0
  #6
May 6th, 2009
ok I had figured out I had to change it to Pow.....was getting that overload error.....i see what ur saying about it ending up dividing by zero.....but I dont know what Im doing....



  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7.  
  8. int main()
  9. {
  10. int number = 0;
  11. int n = 0;
  12. int result = 0;
  13.  
  14. cout << "Enter a number: ";
  15. cin >> number;
  16. cout << "Enter a digit position:";
  17. cin >> n;
  18.  
  19. if ( 0 == number)
  20. {
  21. cout << "0";
  22. }
  23.  
  24. while (n-- > 1)
  25. {
  26. number = number / pow(10,(n-1.0));
  27. result = number % 10;
  28. }
  29.  
  30. cout << "\nThe value of this digit is: " << result << endl;
  31. system("Pause");
  32.  
  33. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Finding the value of a digit

 
0
  #7
May 6th, 2009
>>number = number / pow(10,(n-1.0));
Its still incorrect. 1 is subtracted from the return value of pow(), not from n. See the formula in my previous post
number = number / (pow(10,n) -1 );
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Finding the value of a digit

 
1
  #8
May 7th, 2009
Originally Posted by ddanbe View Post
He's counting his digits from right to left Walt.
So IMHO it has to be more something like this : (pseudocode)

number = 12345;
n = 4; //we seek the fourth digit from the right
num = number / 10^(n-1);
digitvalue = num % 10; //this we want
Interesting....
Dividing by 10, Mod'ing by 10. You just added the obvious power which was hers to think through. I tried not to take all the thinking out of the problem.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 291 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC