Integer manipulation

This is a discussion on Integer manipulation within the C Programming forums, part of the General Programming Boards category; HI ALL, Just got bored and decided to look into a few things. Like, is there a easy way to ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Lightbulb Integer manipulation

    HI ALL,
    Just got bored and decided to look into a few things.
    Like, is there a easy way to reverse an integer and assign that value to another int type?

    Code:
    Example:
    #include <stdio.h>
    main(){
              int grab;
              int reverse;
    printf("\"Enter a five digit integer\"\n");
    scanf("%d", grab);
         if ((grab > 00000)  && (grab <=99999)){
              printf("\"You have entered: %d, great!\"\n");
              }
              else{
                     printf("\"To big a number!\"\n", grab);
              }
    //reverse integer and assign it into reverse
    getche();
    }
    
    __________________________
    Believing no rights are wrong, can be wrong by rights!
    AmpIII
    Here is a quick example of what I was thunkin! Thanks for the help IA!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    One way to reverse the digits of a number would be to count the digits in the number (1), then take each digit from the old number and put it into the right place in the new (2).

    1. Counting the digits in a number.
    Code:
    for(temp = number; temp; temp /= 10, digits ++);
    2a. Extracting a digit.
    Code:
    digit = number / pow(10, num) % 10;
    2b. Assigning a digit.
    Code:
    newnumber = newnumber + digit * pow(10, newnum);
    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.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This should reverse an integer:
    Code:
    int reverse(int get){
        int temp=0,x;
        for(x=get;x!=0;x/=10){
            temp*=10;
            temp+=x%10;
        }
        return temp;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    This has been asked before, quite recently.
    Try a board search.
    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.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Oh, and this checks for overflow, if the number would be too big, it just outputs the highest (or lowest) possible value:
    Code:
    int reverse(int get){
        int temp=0,x;
        __int64 check=0;
        for(x=get;x!=0;x/=10){
            check*=10;
            check+=x%10;
            if(check>std::numeric_limits<int>::max() && get>0){
                return std::numeric_limits<int>::max();
            }
            else if(check*-1<std::numeric_limits<int>::min() && get<0){
                return std::numeric_limits<int>::min();
            }
            else{
                temp*=10;
                temp+=x%10;
            }
        }
        return temp;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You could always mention that __int64 is non-standard.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Wink Thanks!

    Thanks for the comments to all who replied!

    _______________________
    Believeing no rights are wrong, can be wrong by rights!
    AmpIII

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    And std:: things don't work in C... sorry
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

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