Find the last three digits of any power of an integer (precision problem)

topic:

Find the last three digits of an integer raised to any power! The last three digits of 347 raised to the power of 72 is 241.

#include<stdio.h>
int main() {
	int x, y, p, n;
	printf("Description: base is x, power is y, please input:\n");
	printf("x=");
	scanf("%d", &x);
	printf("y=");
	scanf("%d", &y);
	p = 1;
	for(n = 1; n <= y; n++) {
		p=p*x%1000; //The key is to grasp the meaning of this sentence, the last three digits are always only related to the last three digits of its multiplier, so only the last three digits are reserved to avoid overflow!
		printf("%3d: p=%3d \n", n, p);//If the output process is not required, this line of code is not required
	}
	printf("The last three digits of the %d power of %d are: %3d\n", x, y, p);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326039747&siteId=291194637