Help!!!!!

This is a discussion on Help!!!!! within the C++ Programming forums, part of the General Programming Boards category; hi im new at programming, and i need to write a program that takes an integer value and returns the ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    51

    Help!!!!!

    hi im new at programming, and i need to write a program that takes an integer value and returns the number with its digits revered. sooo basically a pallindrome? ex. 12345 would be 54321

    but heres the catch i dont know how many integers the user will enter. so how would i go about writing this code?

    any help is greatly appreciated thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Write a function which repeatedly calculates the least signifigant digit and adds it to a return value, then divide the number by 10. When the value is zero, return the reversed value. The calculation would be something like this:

    ret = ( val % 10 ) + ret * 10;

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    51
    wow ok well when i said iw as new at programming i meant NEW strings we havent coverd thats like chaper 16 lol we are on 3 if anyone who can help me please post your aim handle if possible or contact me mine is gigaflare999 thanks for the replys btw i appreciate it, i just need like serious help lol

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if anyone who can help me please post your aim handle
    I'm hurt , I pretty much gave you the solution without using strings. Just throw it in a loop and you're done:
    Code:
    while ( val != 0 ) {
      ret = ( val % 10 ) + ret * 10;
      val /= 10;
    }
    Now ret is the reversed val.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    51
    sheesh sorry i was lscared when i saw string [256] i ran in other direction! thanks for helpnig i get whiles easy! i appreciate it but will that work no matter how many ints they put in?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i appreciate it but will that work no matter how many ints they put in?
    It will if you place it in a loop.
    Code:
    #include <iostream>
    
    int reverse ( int val )
    {
      int ret = 0;
    
      while ( val != 0 ) {
        ret = ( val % 10 ) + ret * 10;
        val /= 10;
      }
    
      return ret;
    }
    
    int main()
    {
      int num = 0;
    
      std::cout<<"Enter a number (CTRL+Z to quit): ";
      while ( std::cin>>num ) {
        std::cout<<"Your number reversed was: "<<reverse ( num ) <<std::endl;
        std::cout<<"Enter a number (CTRL+Z to quit): ";
      }
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    51
    omg thankyou so much you dont know how much you helped me. really. if you could reccomend any learning sites that are like uber simple please do, my computer teacher sucks ass so bad he doesnt teach.

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