编程实现(整数位数):用户输入一个非负整数,程序在屏幕上输出该数的位数,如果是负数直接输出0。
时间: 2023-05-27 20:01:00 浏览: 304
123,c语言怎样输出一个数的源码,c语言程序
```python
num = int(input("请输入一个非负整数:"))
if num < 0:
print(0)
else:
count = 0
while num != 0:
count += 1
num //= 10
print("该数的位数为:", count)
```
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
if (num < 0) {
System.out.println(0);
} else {
int count = 0;
while (num != 0) {
count++;
num /= 10;
}
System.out.println("该数的位数为:"+count);
}
scanner.close();
}
}
```
```c++
#include <iostream>
using namespace std;
int main() {
int num;
cout << "请输入一个非负整数:";
cin >> num;
if (num < 0) {
cout << 0 << endl;
} else {
int count = 0;
while (num != 0) {
count++;
num /= 10;
}
cout << "该数的位数为:" << count << endl;
}
return 0;
}
```
```c
#include <stdio.h>
int main() {
int num;
printf("请输入一个非负整数:");
scanf("%d", &num);
if (num < 0) {
printf("0\n");
} else {
int count = 0;
while (num != 0) {
count++;
num /= 10;
}
printf("该数的位数为:%d\n", count);
}
return 0;
}
```
阅读全文