intervals and percentage

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int x, y, r, vx, vy, n = 0, n1 = 0, n2 = 0;
	cin >> x >> y;

	if (x >= 0) {
		r = y - x;
	}
	else {
		r = abs(x) + y;
	}

	vx = (r * 30) / 100; //процент 30
	vy = (r * 45) / 100; //процент 45
	for (vx;vx <= vy;vx++) {
		n1 = vx % 10; //1 цифра
		n2 = vx / 10; //2 цифра
		n = (n1 * 10) + n2;
		cout << n << " ";

	}
	system("pause");
	return 0;
}
Last edited on
Have you tried it?

If you do with say input of 10 30 you get:


10 30
60 70 80 90


which doesn't seem correct.

What about if the interval entered is say 1000 1200 ?
Last edited on
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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include <string>
#include <iostream>
#include <algorithm>

int main()
{
	const int 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());

		const auto rn {std::stoi(s)};

		if ((rn >= n + n * inclow / 100.0) && (rn <= n + n * inchgh / 100.0))
			std::cout << n << "  " << rn << '\n';
	}
}


For the interval 10 200 giving:


Enter the range: 10 200
23  32
46  64
57  75
69  96

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace 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
thank you lots guys!! you are life-savers : - )
Last edited on
Who on earth is reporting seeplus and lastchance here? @fidarova is that you?
@MikeyBoy nope, i was wondering too
The signal-to-noise ratio of reports on this forum must be astronomically low.

Meanwhile actual sp@mbots keep getting through...
Last edited on
Topic archived. No new replies allowed.