首页 » 未分类 » 正文

C语言显示整数每一位上的数字

发布者:站点默认
2015/06/25 浏览数(1,565) 分类:未分类 C语言显示整数每一位上的数字已关闭评论

效果:

getPreDig

代码:

/**
	Name: getPreDig.c
	Copyright: none
	Author: upall.cn
	Date: 25-06-15 15:48
	Description: 显示整数每一位上的数字 201506 -> 2、0、1、5、0、6 
*/

#include <stdio.h>
#include <math.h>
unsigned short length = 0;
unsigned int value = 31415; // 第1位不要用0,会被认为是8进制 

int main(){
	unsigned int tempValue = value;
	short i;
	length = log10(tempValue); // 数值的位数(log10 开10次方根) 
	for(i = length; i >= 0; i--){
		unsigned int   powValue = pow(10, i); // 10的i次方 
		unsigned short bitVal = tempValue / powValue;
		printf("%d 的第 %d 位是: %d \n", value, length+1-i, bitVal);
		tempValue -= bitVal * powValue;
	}
}

– 完 –

点击返回顶部
  1. 留言
  2. 联系方式