C语言之路-3-循环

1、while循环计算数字位数

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int n=0;
 6     int x;
 7     printf("请输入数字:");
 8     scanf("%d",&x);
 9     
10     while (x>0){
11         n++;
12         x /= 10;    
13     }
14     
15     printf("%d",n);
16 }

2、do-while循环

do-while先进行一次循环体,再判断条件是否满足

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int n=0;
 6     int x;
 7     printf("请输入数字:");
 8     scanf("%d",&x);
 9     
10     do{
11         n++;
12         x /= 10;
13     }while(x>0);
14     
15     printf("%d",n);
16 }

猜你喜欢

转载自www.cnblogs.com/yinwenjie/p/10328314.html