### Reversed values

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.
 ``12345678910111213141516171819`` ``````#include using namespace std; int main(){ int a[10], sum=0,inv=0; for(int i=0;i<10;i++){ cin>>a[i]; while(a[i]>0){ inv=inv*10+a[i]%10; a[i]=inv; a[i]=a[i]/10; } sum+=a[i]; } cout<

P.S.: I don't need any system("PAUSE") because I'm on Linux right now.
 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
Last edited on
Piece of cake:

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include 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; }``````
Thank you for the help, now it works very gentle.. !
May I ask where this question is?
what do you mean.. i think it could be given in one year at the olympiad but i got it from my info teacher..
Topic archived. No new replies allowed.