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?
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.
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" ?
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;
}
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