08、求x的y的幂次方的最后3位数——循环

本文介绍了一个简单的C语言程序,该程序用于计算任意基数x的指数y的幂次方结果的最后三位数字。通过取模运算,程序有效地处理了大数问题,并展示了如何分离并打印出最后三位数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

求x的y的幂次方的最后3位数

求x的y的幂次方的最后3位数

程序代码如下:

  /*
        2017年3月12日14:07:05
        功能:程序求x的y的幂次方的最后3位数
    */
    #include"stdio.h"
    void fun(int);
    int main()
    {
        int x, y;
        int x_power_y = 1;
        printf("please int two number x and y :");
        scanf("%d %d", &x, &y);
        for (int i = 0; i < y; i++)
        {
            x_power_y *= x;
        }
        if (x_power_y < 999)
        {
            printf("the last three number of the x power y is %d ", x_power_y);
        }
        else
        {
            fun(x_power_y);                                                                    //大于999,进入调用函数
        }

    }

    void fun(int x_power_y)
    {
        int first = x_power_y % 10;                                                            //此时求得是最后一位上的数值
        int second = (x_power_y % 100 - first * 1) / 10;                                    //x_power_y % 100是最后两位上的数值
        int thrid = (x_power_y % 1000 - second * 10 - first * 1) / 100;
        printf("the last three number of the x power y is %d \n", thrid*100+second*10+first);
    }
    /*
        总结;
        在VC++6.0中显示的结果:
        ——————————————————
        please int two number x and y :11 3
        the last three number of the x power y is 331
        ——————————————————
    */

  

转载于:https://www.cnblogs.com/wxt19941024/p/6537808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值