if i have 71, i need to display it like 17. backward
if i have 113 i need to display it like 311. first number and last number are swiched

i know i need to used a swap function but how do i used it? how do i extract each digits? thanks

Recommended Answers

All 9 Replies

Good day...welcome aboard. We don't give away codes...you show some effort for us to help you...here's a jumpstart..pretty much we use the integer division and modulus operations ( / and % ). Its a rather simple assignment.:

#include <iostream>//you first need these headers
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;

int main()//need your main function
{
int number;//declare a few variables you'll need
int reverse;

cout<<"Please enter a number to get it's reverse"<<endl;//ask the user to enter a number
cin>>number;//user input a number

{
	reverse =(123' ' x)*x+(123' 'x)/1x+(123)' 'x;//if i wanted the reverse of 123 do certain calculation

}
cout<<"The reverse of the number is"<<reverse<<endl;//prints out the reverse of the digits

	return 0;
}

You need to go figure what x is...you'll also need to figure out which operator(division or modulus) goes where i have the ' '
Hope this helps...no more spoon-feeding for you...

Actually, the above may not work.....sorry about that..here's a simple algorithim...not sure why your professor wants you to use switch statements..

First, look at the following for 4754:

(Iteration 1)
0 * 10 + 4 = 4

(Iteration 2)
4 * 10 + 5 = 45

(Iteration 3)
45 * 10 + 7 = 457

(Iteration 4)
457 * 10 + 4 = 4574

return 4574

Do you see the pattern? The first number in the equations above is the "current sum". It always starts at 0 and is accumulated in the while loop. Each "subanswer" is the new sum to be used in the next equation.
Hope that helps.

Make a loop that exits when your value is zero
In the loop, 
    calculate the modulo-10 of the value using % (get the ones digit)
    output the digit
    divide the number by 10
endloop

That's basically it.

> calculate the modulo-10 of the value using % ...
the value may be negetive; you need to adjust for that.

al alternative is
a. convert the number to a string
b. reverse the string
c. convert the string back to a number.
eg.

int reverse( int number )
{
  bool negetive = number < 0 ;
  if( negetive ) number = -number ;
  
  ostringstream ostm ;
  ostm << number ;
  string str = ostm.str() ;
  // c++0x => string str = lexical_cast<string>(number) ;
    
  reverse( str.begin(), str.end() ) ;

  istringstream istm(str) ;
  int result ;
  istm >> result ;
  // c++0x => int result = lexical_cast<int>(str) ;

  return negetive ? -result : +result ;
}

ok, thanks guys.

i think i am going to use the mod 10 step. will post again if i need more help.

two new question on something else.
1)
i am doing a calender program, and the user inputs the first date of that year and the year, and a month. and i am suppose to print out the just that particular month correctly.

here is what i have thought of so far.
user input a year if it is a leap year, it will give me 366 day.
and the first date of that year the user enter will be like 1 for sunday, 2 for monday....

and the print looks like this

S M T W T F S
X X X X X X X

i will use a loop that will print 31 intergers and no more than 7 dates on a line. i will use the number the user input and if it is 1 for monday it will be like one X block. i will just start printing number on second block, but my question is how do a loop that will give me correct block on let said September. i need some how pass the block number to the next month and so forward. thanks.

2)

i have another program that ask the user to input a number, i have set up validation loop
like cin.good() and cin.clear(). but the thing is when ever user inputs a number a variable the validation is not doing it's job.

ex: input: ababa , output: bad input
ex: input: 5a, output: nothing, skips my while loop and go on to the next prompt?
why, thanks again.

looking forward for you guys help thanks.

1) Calculate the number of days to Sept 1, add the day of the week for Jan 1, divide by 7. Remainder should be the day of the week for Sept 1

2) Line 46 of your validation function should be < not ==. IOW, how are we supposed to know? we ain't psychic! :icon_wink:

question on the modules % 10;
if i use % 10 on 17 it will only return 7 as the result. how do i get the first digit again.

Divide by 10 and use % again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.