天天看点

任意次方后的最后三位

C语言(输出任意次方后的最后三位)

    • 对判断该数位数的函数改进
#include <stdio.h>
#include <math.h>
//输入数字的位数
int what_num(int a)
{
	if (a/100>0)
	{
		return 3;
	} 
	else if (a/10>0)
	{
		return 2;
	}
	else 
	{
		return 1;
	}
}
//  求最后三位数字
int lasthreenum(int b,int d)
{
	int c = 0;
	if (3==b)
	{
		c = d%1000;
		if (0==c)
		{
			printf("last three num is 000");
		}
		else
		{
			printf("last three number is %d",c);
		}
		
	}
	else if (2==b)
	{
		c = d%100;
		if (0==c)
		{
			printf("last two num is 00");
		}
		else
		{
			printf("last two number is %d",c);
		}
		
	}
	else
	{
		printf("last number is %d",c);
	}
	return 0;
}

int main()
{
	int n;
	printf("please enter the number:\n");
	scanf("%d",&n);
	n = (int)pow(n,3);
	printf("The num's digit great than or equal to%d\n",what_num(n));
	lasthreenum(what_num(n),n);
	return 0;
}
           

对判断该数位数的函数改进

int what_num(int a)
{
	int i =1;
	while (a/10!=0)
	{
		a = a/10;
		i++;
	}
	return i;
}