I have a problem where in a range of numbers L to R I should check how many of them are both palindroms (read the same forward and backward) OR divisible by all of its digits. Now, I have started a program which doesn't really work well, I made a few functions but I have a problem where in line 30 the compiler calls an error saying:
Main.cpp:30:41: error: in evaluation of 'operator%=(int, __gnu_cxx::__promote_2::__type {aka double})' palindrom_check %= pow(10, upper); ^
Here are examples of input and output given in the problem:
- Input: 1 15
- Output: 12
Explanation: In the interval of 1 to 15, the numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15 - therefore the output is 12.
- Input: 303 304
- Output: 1
I am pretty sure I'm not using the pow() correctly. I hope anyone can give me a way to continue or edit my code to fix it and make it work.
#include <iostream>
#include <cmath>
using namespace std;
int digits = 1;
int check_dig = 0;
int Digit(int n) {
digits = 1;
check_dig = n;
while (check_dig > 10) {
check_dig /= 10;
digits++;
}
}
int exponent = 1;
int front_check = 0;
int back_check = 0;
bool palindrom = false;
int palindrom_check = 0;
int upper = digits - 1;
int Palindrom(int n) {
palindrom = false;
exponent = (int)pow(10, upper);
front_check = n;
palindrom_check = n;
while (palindrom_check > 0) {
back_check += (palindrom_check % 10) * exponent;
palindrom_check %= exponent;
exponent /= 10;
}
if (back_check == front_check) {
palindrom = true;
}
}
bool divisible = false;
int divisible_check = 0;
int Divisibility(int n) {
divisible_check = n;
while (divisible_check > 0) {
if (divisible_check % (divisible_check % 10) == 0) {
divisible_check /= 10;
divisible = true;
continue;
} else {
divisible = false;
break;
}
}
}
int main() {
int L, R;
cin >> L >> R;
int result = 0;
for (int i = L; i <= R; i++) {
Digit(i);
Palindrom(i);
Divisibility(i);
if (palindrom || divisible) {
result++;
}
}
cout << result;
return 0;
}
Edit:
#include <iostream>
#include <cmath>
using namespace std;
int digits = 1;
int check_dig = 0;
void Digit(int n) {
digits = 1;
check_dig = n;
while (check_dig > 10) {
check_dig /= 10;
digits++;
}
}
int front_check = 0;
int back_check = 0;
int palindrom_check = 0;
int upper = digits - 1;
bool palindrom(int n) {
front_check = n;
back_check = 0;
palindrom_check = n;
while (palindrom_check > 0) {
back_check = (back_check * 10) + (palindrom_check % 10);
palindrom_check /= 10;
}
if (back_check == front_check) {
return true;
}
}
int divisible_check = 0;
bool divisible(int n) {
divisible_check = n;
while (divisible_check > 0) {
if (divisible_check % (divisible_check % 10) == 0) {
divisible_check /= 10;
return true;
} else {
return false;
break;
}
}
}
int main() {
int L, R;
cin >> L >> R;
int result = 0;
for (int i = L; i <= R; i++) {
Digit(i);
palindrom(i);
divisible(i);
if (palindrom || divisible) {
result++;
}
}
cout << result;
return 0;
}