# Thread: help with a c++ program

1. ## help with a c++ program

Hello !

I need some help with this code(my program should display the last digit of the sum
2^a + 3^a +...+9^a - where a is a number entered from the keyboard):
Code:
```#include <iostream>
using namespace std;

int main()
{
int a,n,i,p = 1,uc,s = 0;
cout << "a = ";
cin >> a;
if (a == 0) cout << "8" << endl;

for (n = 2;n <= 9;n++) {
for (i = 1;i <= n;i++) {
p = p*n;
uc = p%10;
p = p%10;
s = s+uc;
}
}
cout << s << endl;
return 0;
}```
What's wrong with my code ? The program doesn't work.
I would be grateful if you would explain to me what I did wrong.

: 2. For one thing, none of your calculations involve a. You rather seem to be adding 2^1 + 2^2 + 3^1 + 3^2 + ... + 9^8 + 9^9 (except that you keep accumulating things to the same p). 3. Originally Posted by anon For one thing, none of your calculations involve a. You rather seem to be adding 2^1 + 2^2 + 3^1 + 3^2 + ... + 9^8 + 9^9 (except that you keep accumulating things to the same p).
My first idea was to add the last digit of every term of the sum.
By summing the digits I thought I could find out the last digit of the number given.
For example,if a is 2 the number 2^4 + 3^4 = 6 + 1 =7.
I now realise that for bigger numbers the sum will exceed 10 so I need to calculate the last digit of this one too.
What should I do? 4. Run a loop, varying n from 2 to 9 and adding pow(n,a) to the result each time. 5. Thank you ! It works now very well  Popular pages Recent additions 