| | |
Finding the value of a digit
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
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");
} >>num = number / (10^n-1);
you have to use pow() function
The problem with that is if pow() returns 1 then the formula will get a divide by 0 error.
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.
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
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....
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using std::cin; using std::cout; using std::endl; int main() { 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) { number = number / pow(10,(n-1.0)); result = number % 10; } cout << "\nThe value of this digit is: " << result << endl; system("Pause"); }
>>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
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.
•
•
•
•
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
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Logical error in finding Palindromes (C++)
- ASP - Finding a Specific Number in an Integer (ASP)
- Factorial of any length (C++)
- Help with program to convert hex to dec (Assembly)
- Finding length (Java)
Other Threads in the C++ Forum
- Previous Thread: system wide api hook
- Next Thread: Left Right Binary Exponentiation
Views: 291 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






