write a program to print the sum and average of all the palindromic numbers from 2 to 2000 in c++
Answers
Answer:
Here's a program in C++ to print the sum and average of all the palindromic numbers from 2 to 2000:
```
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int count = 0;
for (int i = 2; i <= 2000; i++) {
int num = i;
int rev = 0;
while (num > 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}
if (rev == i) {
sum += i;
count++;
}
}
float average = (float) sum / count;
cout << "Sum of palindromic numbers from 2 to 2000: " << sum << endl;
cout << "Average of palindromic numbers from 2 to 2000: " << average << endl;
return 0;
}
```
This program uses a for loop to iterate through all the numbers from 2 to 2000. For each number, it checks if it is a palindrome by reversing the digits of the number and comparing it to the original number. If it is a palindrome, it adds it to the sum and increments the count. Finally, it calculates the average by dividing the sum by the count and prints both the sum and average.