十进制转二进制-使用do while 、while 、for循环实现-C描述
2017-02-10 10:03
239 查看
十进制转二进制-使用do while 、while 、for循环实现
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <math.h> //位转换 /* 6%2 = 0 3%2 = 1 1%2 = 1 */ void 十进制转二进制() { printf("输入十进制\n"); int *pNum = (int*)malloc(1 * sizeof(int)); scanf("%d", pNum); //数组 int * pResult = (int *)malloc(1000 * sizeof(int)); printf("二进制是:\n"); int count = 0; /*do { //使用do while方式 *(pResult + count) = *pNum % 2; printf("count = %d\n", count); count++; *pNum = *pNum / 2; } while (*pNum);*/ //while (*pNum) { //使用 while方式 // *(pResult + count) = *pNum % 2; // printf("count = %d\n", count); // count++; // *pNum = *pNum / 2; //} for (; *pNum; count++) { //使用For循环方式 *(pResult + count) = *pNum % 2; printf("count = %d\n", count); *pNum = *pNum / 2; } printf("count->%d\n", count); for (int i = count - 1; i >= 0; i--) { printf("%d", *(pResult + i)); } printf("\n"); free(pNum); free(pResult); }
相关文章推荐
- 不使用for、while等循环实现strlen()函数
- 使用for、while 以及repeat-break实现循环语句
- c++中的结构化语句 判断语句if 分支语句switch 循环语句 while 和 do while 循环语句for的使用
- 实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字(for、while、if、else、switch、case)。
- java中循环命令while,for,do while的区别
- c循环语句for,while,do while,goto
- 从键盘输入一组非0整数,以输入0为结束标志,求这组整数的平均值,并统计其中正数和负数的个数。(根据循环变量的初值、终值无法判断出循环次数时建议使用while或do while语句完成)。
- 循环语句的使用——while、do_while、for
- (AS3)在循环语句for、for each、while等语句中使用闭包需要注意的地方
- c语言中不用while,for,do-while实现循环
- 黑马程序员_java编程基础第3天1-5 循环结构while;do while;for
- 不使用乘除法,for,while,if,else,switch,case,条件判断语句(A?B:C) 实现:1+2+....+n
- Swift学习——使用if和switch来进行条件操作,使用for,while,和do-while来进行循环(三)
- java之图案拼接-for循环和while的使用
- 黑马程序员_java编程基础第3天1-5 循环结构while;do while;for
- 使用while或do while循环输出从0到9十个数字
- C#分别使用for,while,do-while循环方法求的n!
- .NET入门(九)测试各种循环结构,for,while和do while
- JS:2.2,循环控制(JavaScript,for,while,do while,break,continue)高级
- for循环和while循环在使用continue时有所区别