【cpp练习题】编写函数digit(num, k),函数功能是:求整数num从右边开始的第k位数字的值,如果num位数不足k位则返回0

直接上code:

#include <stdio.h>
#include<math.h>
#include<iostream>

using namespace std;

int digit(int num, int k);
int num;
int k;


int main ()
{
	cout << "please enter your number" << endl;
	cin >> num;
	cout << "plase enter the number of the site you wanted" << endl;
	cin >> k;
	digit(num, k);
	return 0;


}

int digit(int num, int k)
{
	if (num == 0)
	{
		cout << "you've input the wrong number" << endl;

	}
	else if(k==0)
	{
		cout << "the number of site you wanted must be with value" << endl;

	}
	else
	{
		int temp;
		temp=num % ((int)pow(10,k));
		if (temp > 10)
		{
			temp = temp / 10;
		}

		cout << "the site fo the number you wanted is:" << temp << endl;
	}
	return 0;
}

测试结果截图:

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值