Hello everyone! I have a problem with a 9th grade olympiad question and i wonder if any of you could help....
I have to read 10 values from the console and reverse their digits, displaying the sum afterwards.
I have to read 10 values from the console and reverse their digits, displaying the sum afterwards.
i would think the easiest way is to read the values in as a string, reverse the string, convert them to int/double and then get the sum of the new numbers
#include <iostream>
int reverse_digits(int n)
{
int r = 0;
while (n != 0)
{
r *= 10;
r += n % 10;
n /= 10;
}
return r;
}
int main()
{
std::cout << "Enter 10 values:" << std::endl;
int a[10];
for (int i = 0; i < 10; i++)
{
std::cin >> a[i];
}
int sum = 0;
for (int i = 0; i < 10; i++)
{
sum += reverse_digits(a[i]);
}
std::cout << sum;
return 0;
}