help me out

This is a discussion on help me out within the C Programming forums, part of the General Programming Boards category; i want a method too get the last two digit from a number like 299 then i should get 99 ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    help me out

    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

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    er, not sure, but try this:

    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 wrote this in C++, but if you transfer it into C it should operate the same.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    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)

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Quote Originally Posted by Epo
    Subtraction is a pretty quick method though no?

    Why not just keep on subtracting 100 while your number is greater than 100?

    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...

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by Epo
    Subtraction is a pretty quick method though no?

    Why not just keep on subtracting 100 while your number is greater than 100?
    O(1) is faster than O(N).
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    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?.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

  9. #9
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    O(1) is faster than O(N).
    Depends on the size of N in this case, no?

    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)

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    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.

  11. #11
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    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)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21