the guestion says: find in the given by the user interval (im guessing they mean (12;28) etc?) all the natural numbers which will increase by 30-45% if you write them in reverse (23 written as 32 etc)
my friend and i have the same task and he sent me the solution, but i dont really get it. could you please check if he's correct and explain how he got these?
To compare a number and its reverse, there needs to be a way of reversing them. Once simple way is to convert to string, reverse the string and then convert to number (without input error checking):
include <string>
#include <iostream>
#include <algorithm>
int main()
{
constint inclow {30}, inchgh {45};
int from {}, to {};
std::cout << "Enter the range: ";
std::cin >> from >> to;
for (int n = from; n <= to; ++n) {
auto s {std::to_string(n)};
std::reverse(s.begin(), s.end());
constauto rn {std::stoi(s)};
if ((rn >= n + n * inclow / 100.0) && (rn <= n + n * inchgh / 100.0))
std::cout << n << " " << rn << '\n';
}
}
#include <iostream>
usingnamespace std;
int rev( int n )
{
int back = 0;
while( n )
{
back = 10 * back + n % 10;
n /= 10;
}
return back;
}
int main()
{
int a, b;
cout << "Enter (positive) a and b for CLOSED interval [a,b]: "; cin >> a >> b;
for ( int n = a; n <= b; n++ )
{
int test = 20 * rev( n );
if ( test >= 26 * n && test <= 29 * n ) cout << n << '\n';
}
}
Enter (positive) a and b for CLOSED interval [a,b]: 12 28
23