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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

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<<sum<<endl;
	return 0;
}	


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:

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
28
29
30
31
32
33
34
35
36
37
38
#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;
}
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.