GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Apr-2007, 09:08
tootoo tootoo is offline
New Member
 
Join Date: Apr 2007
Posts: 4
tootoo is an unknown quantity at this point

Reverse a number or digits


Write program that reverse a number digits. The program will ask the user to enter a number (one variable) consist from three digits then the program reverse the digits of the number.
Note: you must define the number as integer. To solve the problem use the / and the %

The program output might be like this

Enter any number consist from three digits = 123
The reverse of this number= 321
  #2  
Old 06-Apr-2007, 09:27
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: reverse a number digits


You will probably want to post the code you are using. You will be more likely to get help if you show your work first. Members are much more likely to jump in and help than to do your work for you.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 06-Apr-2007, 10:50
tootoo tootoo is offline
New Member
 
Join Date: Apr 2007
Posts: 4
tootoo is an unknown quantity at this point

Re: reverse a number digits


CPP / C++ / C Code:
#include <iostream>


int main ()
{
short x;

std::cout<<"enter a number";
std::cin>>x;

    short a=(x%10);
    short b=(?%?)?10;  
    short c=(x/?);

std::cout<<?<<?<<?<<std::endl;
   
    std::cin.ignore();
	std::cin.get();

    
    return 0;
}
  #4  
Old 06-Apr-2007, 11:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: reverse a number digits


Quote:
Originally Posted by tootoo
CPP / C++ / C Code:
#include <iostream>
.
.
Posting a hint that you got from another forum is really bad form. Why don't you try it yourself?

Show something that you wrote. If it doesn't compile, then tell us exactly what the messages were. If it compiles but doesn't give the answers you expected, then tell us what you did get and what you didn't understand about it.

Regards,

Dave
  #5  
Old 06-Apr-2007, 12:46
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: reverse a number digits


Taking the code you posted and commenting out all the lines with a question mark gives me...

CPP / C++ / C Code:
#include <iostream>

int main ()
{
  short x;
  
  std::cout<<"enter a number";
  std::cin>>x;
  
  short a=(x%10);
//   short b=(?%?)?10;
//   short c=(x/?);

//   std::cout<<?<<?<<?<<std::endl;

  std::cin.ignore();
  std::cin.get();

  
  return 0;
}
Since all that happens when you do this is you are prompted for a number then have to hit a key to quit. Add a cout with the value you got for a and see if it works.

CPP / C++ / C Code:
  std::cout << "a = " << a << std::endl;

It seems you got the least significant digit, Bravo! Now, you are going to want to use your short (int) and dividing by 10 to your advantage. Once you do that (and get the right answer) you just use the same method you used on a to find the new least significant digit.

Rinse, Lather, Repeat

Then just structure your output statement with the proper variables.

At least that seems to be the assignment to me.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #6  
Old 22-Jul-2007, 08:08
surya.k surya.k is offline
New Member
 
Join Date: Aug 2006
Posts: 14
surya.k is on a distinguished road

Re: Reverse a number or digits


wht they said is really good,
a standard logic to start working on it 4 u,..
CPP / C++ / C Code:
int r=0;
while(x!=0)
 {
  d=x%10;
  r=(r*10)+d;
  x=x/10;
 }
std::cout<<r;

regrards
surya
Last edited by LuciWiz : 22-Jul-2007 at 09:33. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #7  
Old 29-Jul-2007, 20:34
davis
 
Posts: n/a

Re: Reverse a number or digits


Quote:
Originally Posted by tootoo
Write program that reverse a number digits. The program will ask the user to enter a number (one variable) consist from three digits then the program reverse the digits of the number.
Note: you must define the number as integer. To solve the problem use the / and the %

The program output might be like this

Enter any number consist from three digits = 123
The reverse of this number= 321


Maybe something like:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main()
{
    cout << "Enter a positive number: ";
    int number;
    cin >> number;
    if( number > 0 )
    {
        do
        {
            cout << (char)( '0' + (number % 10) );
            number /= 10;
        } while( number != 0 );
        cout << endl;
    }
    else
    {
        cout << "Number not positive!" << endl;
    }
    return 0;
}

Output:

Code:
Enter a positive number: 123 321 Enter a positive number: 448737 737844 Enter a positive number: -123 Number not positive!


:davis:
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a number amount to text Godzilla C++ Forum 5 31-Mar-2006 11:38
Repeated digits in a number sequence pedro_21 Java Forum 1 01-Nov-2005 15:03
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Breaking the number into two digits amir_b C Programming Language 3 16-Feb-2004 12:28
How to find the last 6 digits of a 8 digit number? rjd72285 C++ Forum 3 28-Oct-2003 08:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.