i want a method too get the last two digit from a number
like 299 then i should get 99
or 178 then 78
plz if u know reply soon
thanks in advance
i want a method too get the last two digit from a number
like 299 then i should get 99
or 178 then 78
plz if u know reply soon
thanks in advance
er, not sure, but try this:
I wrote this in C++, but if you transfer it into C it should operate the same.Code:#include <iostream> using namespace std; int number; int main() { cout << "Please type in a three digit number: "; cin >> number; number=number- ; // this part you would minus what ever the first figure was, ie: if it // was 150 then take away 100 cout << "You typed in " << number; // this would display your outcome cin.get(); cin.ignore(); return 0; }
I'm sure the % operator would come in handy here
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Subtraction is a pretty quick method though no?
Why not just keep on subtracting 100 while your number is greater than 100?
Pentium 4 - 2.0GHz, 512MB RAM
NVIDIA GeForce4 MX 440
WinXP
Visual Studio .Net 2003
DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)
Originally Posted by Epo
yes, you can do it.. but its just complecating the matter..
Like salem said it..
take the number.. and do a modulo (%) 100.. so it will give you the reminder.. which is less than 100 i.e ..the last 2 digits.. !!
isn't it easy...
O(1) is faster than O(N).Originally Posted by Epo
If you understand what you're doing, you're not learning anything.
just use (variable)%100 to get last two digits.use type casting (int)variable.if ur variable is float.as told by others.got it my hindu brother?.
Code:#include <stdio.h> int main(void) { int n; printf("\nEnter a number: "); scanf("%i", &n); printf("\nNumber: %i\n", n % 100); return 0; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Depends on the size of N in this case, no?O(1) is faster than O(N).
Compared to one subtraction, one modulus is a fairly expensive computation.
I guess Modulus would be superior for small values of N, but only if there aren't many calculations. If the number stays in the 100s, but must be calculated for 100,000,000 numbers, I would say subtraction might be faster (I have no real data to work from here, but I might test it out just for fun).
For numbers that are big though, I would say no contest that Modulus wins (I.e. 100,890,324).
So yes, in most cases I would say modulus would be faster here, and yeah, I see it's the better choice. There is the once scenario though (which probably isn't what's happening), and, if nothing else, it's just another way to approach the problem.
Pentium 4 - 2.0GHz, 512MB RAM
NVIDIA GeForce4 MX 440
WinXP
Visual Studio .Net 2003
DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)
You're not just dealing with subtraction though. You're dealing with a whole additional loop which means at least a while(n > 99) and that check takes time as well. You have to take all of that into account.
If you understand what you're doing, you're not learning anything.
Very true, very true. I don't know the cost that would create, but MOD is definitely becoming superior by the second
Pentium 4 - 2.0GHz, 512MB RAM
NVIDIA GeForce4 MX 440
WinXP
Visual Studio .Net 2003
DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)