Reversing Numbers

I can only use For loops to solve this problem?



Last edited on
Is that a question?
If "yes", then the answer is "no, there are other methods too".
If "no" as in "that is given restriction for the homework", then what is the question?
I am sorry, How would I reverse numbers using while loops, like 123 and prints out 321.
Last edited on
WHILE there is number left
DO separate the least significant digit from the number and print it
DONE

Operators % and /=are useful.
Last edited on
I do not know what the least significant mean?
I understand what you are saying but what does that have to do with reversing the number
You want to split the 123 into 3 and 12, and then do something with the 3.
Then split the 12 into 2 and 1 and do something with the 2.
Then split the 1 into 1 and null and do the thing with the 1.
ok(?) but does this work for any positive number that the user inputs?
int i=5992;
string s = to_string(i);
now print backwards!
I have no knowledge of printing it backwards
thats reversing string. reversing by significant digits is slightly trickier version of it.
didn't you reverse string before, i.e. "abc def" --> "fed cba" ?
ok(?) but does this work for any positive number that the user inputs?

Close enough.

I did hint hat operators % and / are useful. Lets see what they can do:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <iomanip>

int main() {
  using std::setw;

  for ( int num = 0; num < 99; ++num ) {
    std::cout << setw(3) << num << setw(5) << (num / 10) << '-' << (num % 10) << '\n';
  }
  return 0;
}

Do you notice a pattern in the output of that program?
1). Get the number as an int from std::cin
2). Convert the number to a std::string with std::to_string()
3). Create another std::string using reverse iterators to the previously created std::string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
	std::cout << "Please enter a number:" << std::endl;
	int num = 0;
	std::cin >> num;

	const std::string num_str = std::to_string(num);
	const std::string reversed_num_str(num_str.rbegin(), num_str.rend());
	std::cout << "Here's the number reversed: " << reversed_num_str << std::endl;

	return 0;
}
Last edited on
Let me tell you that I came from highschool and I have never learned how to program. Right now I am starting to learn the basics.
Lets check whether we are all clear about the task.

Do you:
(a) Read a positive integer OR (b) read a word?

Do you want to:
(a) Calculate a "reverse" integer OR (b) reverse the characters of a word?
a and a
Ok. The program should thus be quite simple. It has three logical steps:
1. Read input
2. Calculate result (the while loop)
3. Show result

You need two variables from start, one for storing the input and another for storing the result.

Please write us a program that has at least the first and last steps and show it with code tags (like I and benbalach have done). When you have done that, we can start looking at the second step.
#include<iostream>
#include <string>
int main(){
std::string a;
int x=10;
int num;
std::cout<<"Enter a Positive Integer: ";
std::cin>>num;
while(num>0)
{
if(num<0){
std::cout<<"Enter a postive number\n";
}
else if(num>0){
std::cout<<"\n Reversed Integer is: ";
do{ std::cout<<(num%x)/(x/10); //num modulo x is to obtain the unit digits and dividing by 10 to set up the number for the next iteration
x*=10;
}while((num*10)/x!=0); //num is multiplied by ten to continue as long as the denominator is zero

}
}



return 0;
}
Please add the code tags to your post. See http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.