Yes of cause the upstair solution is OK, but if you want to know why the number like that in array "sam" like that, you can reference my LastBitNumber function.
This function is applied to any modulo not only 10, I've test my program just now, and fix some bug, you can press enter and press controll + Z to end the input. here is the program
#include "stdafx.h"
#include
#include
using namespace std;
// actually it can calculate any modulo last bit, here called this function with modulo = 10
unsigned int Fun_LastBitNumber(unsigned int a, long int exponet, unsigned int modulo)
{
long int i = 0;
unsigned int remainder = 0, temp = 1, remainder_a = 0;
remainder_a = a % modulo;
for (i = 0; i < exponet; i++)
{
temp *= remainder_a;
remainder = temp % modulo;
temp = remainder;
}
return remainder;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int a = 0, lastbitNumber = 0, modulo = 10;// binary octal hexadecimal are also OK;
long int exponet = 0;
std::vector vecLastBitNum;
std::vector::iterator itervecLastBitNum;
while(cin >> a >> exponet)
{
lastbitNumber = Fun_LastBitNumber(a, exponet, modulo);
vecLastBitNum.push_back(lastbitNumber);
}
//Output
itervecLastBitNum = vecLastBitNum.begin();
while( itervecLastBitNum != vecLastBitNum.end() )
{
cout << *itervecLastBitNum << endl;
itervecLastBitNum++;
}
return 0;
}