计算用户输入的长整数中的位数。
简单的迭代解决方案
用户输入的整数存储在变量n中。然后,迭代while循环,直到将测试表达式n!= 0评估为0(假)。
- 第一次迭代后,n的值为345,并且计数增加到1。
- 在第二次迭代后,n的值为34,并且计数增加到2。
- 在第三次迭代之后,n的值为3,并且计数增加到3。
- 在第四次迭代开始时,n的值将为0,并且循环终止。
然后,将测试表达式评估为false并终止循环。
C++
// Iterative C++ program to count
// number of digits in a number
#include
using namespace std;
int countDigit(long long n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
// Driver code
int main(void)
{
long long n = 345289467;
cout << "Number of digits : " << countDigit(n);
return 0;
}
// This code is contributed
// by Akanksha Rai
C
// Iterative C program to count number of
// digits in a number
#include
int countDigit(long long n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
// Driver code
int main(void)
{
long long n = 345289467;
printf("Number of digits : %d", countDigit(n));
return 0;
}
Java
// JAVA Code to count number of
// digits in an integer
class GFG {
static int countDigit(long n)
{
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
/* Driver code */
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : "
+ countDigit(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Iterative Python program to count
# number of digits in a number
def countDigit(n):
count = 0
while n != 0:
n //= 10
count += 1
return count
# Driver Code
n = 345289467
print("Number of digits : % d" % (countDigit(n)))
# This code is contributed by Shreyanshi Arun
C#
// C# Code to count number of
// digits in an integer
using System;
class GFG {
static int countDigit(long n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
/* Driver code */
public static void Main()
{
long n = 345289467;
Console.WriteLine("Number of"
+ " digits : " + countDigit(n));
}
}
// This code is contributed by anuj_67.
PHP
Javascript
C++
// Recursive C++ program to count number of
// digits in a number
#include
using namespace std;
int countDigit(long long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
// Driver code
int main(void)
{
long long n = 345289467;
cout << "Number of digits :" << countDigit(n);
return 0;
}
// This code is contributed by Mukul Singh.
C
// Recursive C program to count number of
// digits in a number
#include
int countDigit(long long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
// Driver code
int main(void)
{
long long n = 345289467;
printf("Number of digits : %d", countDigit(n));
return 0;
}
Java
// JAVA Code to count number of
// digits in an integer
import java.util.*;
class GFG {
static int countDigit(long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
/* Driver code */
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : "
+ countDigit(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Recursive Python program to count
# number of digits in a number
def countDigit(n):
if n == 0:
return 0
return 1 + countDigit(n // 10)
# Driver Code
n = 345289467
print("Number of digits : % d" % (countDigit(n)))
# This code is contributed by Shreyanshi Arun
C#
// C# Code to count number of
// digits in an integer
using System;
class GFG {
static int countDigit(long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
/* Driver Code */
public static void Main()
{
long n = 345289467;
Console.WriteLine("Number of "
+ "digits : "
+ countDigit(n));
}
}
// This code is contributed by anuj_67.
PHP
Javascript
C++
// Log based C++ program to count number of
// digits in a number
#include
using namespace std;
int countDigit(long long n) {
return floor(log10(n) + 1);
}
// Driver code
int main(void)
{
long long n = 345289467;
cout << "Number of digits : "
<< countDigit(n);
return 0;
}
// This code is contributed by shivanisinghss2110
C
// Log based C program to count number of
// digits in a number
#include
#include
int countDigit(long long n) {
return floor(log10(n) + 1);
}
// Driver code
int main(void)
{
long long n = 345289467;
printf("Number of digits : %d", countDigit(n));
return 0;
}
Java
// JAVA Code to count number of
// digits in an integer
import java.util.*;
class GFG {
static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
/* Driver code */
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : "
+ countDigit(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Log based Python program to count number of
# digits in a number
# function to import ceil and log
import math
def countDigit(n):
return math.floor(math.log10(n)+1)
# Driver Code
n = 345289467
print("Number of digits : % d" % (countDigit(n)))
# This code is contributed by Shreyanshi Arun
C#
// C# Code to count number of
// digits in an integer
using System;
class GFG {
static int countDigit(long n)
{
return (int)Math.Floor(Math.Log10(n) + 1);
}
/* Driver code */
public static void Main()
{
long n = 345289467;
Console.WriteLine("Number of digits : "
+ countDigit(n));
}
}
// This code is contributed by anuj_67..
PHP
Javascript
C++
#include
using namespace std;
// To count the no. of digits in a number
void count_digits(int n)
{
// converting number to string using
// to_string in C++
string num = to_string(n);
// calculate the size of string
cout << num.size() << endl;
}
//Driver Code
int main()
{
// number
int n = 345;
count_digits(n);
return 0;
}
// This code is contributed by Shashank Pathak
Java
import java.util.*;
public class GFG {
// To count the no. of digits in a number
static void count_digits(int n)
{
// converting number to string using
// to_string in C++
String num = Integer.toString(n);
// calculate the size of string
System.out.println(+num.length());
}
// Driver code
public static void main(String args[])
{
// number
int n = 345;
count_digits(n);
}
}
// Code is contributed by shivanisinghss2110
Python3
# Python3 implementation of the approach
def count_digits(n):
n = str(n)
return len(n)
# Driver code
n = 456533457776
print(count_digits(n))
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG {
// To count the no. of digits in a number
static void count_digits(int n)
{
// converting number to string using
// to_string in C#
string num = Convert.ToString(n);
// calculate the size of string
Console.WriteLine(+num.Length);
}
// Driver Code
public static void Main(string[] args)
{
// number
int n = 345;
count_digits(n);
}
}
// This code is contributed by shivanisinghss2110
输出
Number of digits : 9
递归解决方案:
C++
// Recursive C++ program to count number of
// digits in a number
#include
using namespace std;
int countDigit(long long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
// Driver code
int main(void)
{
long long n = 345289467;
cout << "Number of digits :" << countDigit(n);
return 0;
}
// This code is contributed by Mukul Singh.
C
// Recursive C program to count number of
// digits in a number
#include
int countDigit(long long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
// Driver code
int main(void)
{
long long n = 345289467;
printf("Number of digits : %d", countDigit(n));
return 0;
}
Java
// JAVA Code to count number of
// digits in an integer
import java.util.*;
class GFG {
static int countDigit(long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
/* Driver code */
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : "
+ countDigit(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Recursive Python program to count
# number of digits in a number
def countDigit(n):
if n == 0:
return 0
return 1 + countDigit(n // 10)
# Driver Code
n = 345289467
print("Number of digits : % d" % (countDigit(n)))
# This code is contributed by Shreyanshi Arun
C#
// C# Code to count number of
// digits in an integer
using System;
class GFG {
static int countDigit(long n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
/* Driver Code */
public static void Main()
{
long n = 345289467;
Console.WriteLine("Number of "
+ "digits : "
+ countDigit(n));
}
}
// This code is contributed by anuj_67.
的PHP
Java脚本
输出
Number of digits :9
基于日志的解决方案:
我们可以使用log10(以10为底的对数)来计算正数的位数(未为负数定义对数)。
N的数字计数= log10(N)的上限。
C++
// Log based C++ program to count number of
// digits in a number
#include
using namespace std;
int countDigit(long long n) {
return floor(log10(n) + 1);
}
// Driver code
int main(void)
{
long long n = 345289467;
cout << "Number of digits : "
<< countDigit(n);
return 0;
}
// This code is contributed by shivanisinghss2110
C
// Log based C program to count number of
// digits in a number
#include
#include
int countDigit(long long n) {
return floor(log10(n) + 1);
}
// Driver code
int main(void)
{
long long n = 345289467;
printf("Number of digits : %d", countDigit(n));
return 0;
}
Java
// JAVA Code to count number of
// digits in an integer
import java.util.*;
class GFG {
static int countDigit(long n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
/* Driver code */
public static void main(String[] args)
{
long n = 345289467;
System.out.print("Number of digits : "
+ countDigit(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Log based Python program to count number of
# digits in a number
# function to import ceil and log
import math
def countDigit(n):
return math.floor(math.log10(n)+1)
# Driver Code
n = 345289467
print("Number of digits : % d" % (countDigit(n)))
# This code is contributed by Shreyanshi Arun
C#
// C# Code to count number of
// digits in an integer
using System;
class GFG {
static int countDigit(long n)
{
return (int)Math.Floor(Math.Log10(n) + 1);
}
/* Driver code */
public static void Main()
{
long n = 345289467;
Console.WriteLine("Number of digits : "
+ countDigit(n));
}
}
// This code is contributed by anuj_67..
的PHP
Java脚本
输出
Number of digits : 9
方法4:
我们可以将数字转换成字符串,然后找到字符串的长度来获得在原号码的位数。
C++
#include
using namespace std;
// To count the no. of digits in a number
void count_digits(int n)
{
// converting number to string using
// to_string in C++
string num = to_string(n);
// calculate the size of string
cout << num.size() << endl;
}
//Driver Code
int main()
{
// number
int n = 345;
count_digits(n);
return 0;
}
// This code is contributed by Shashank Pathak
Java
import java.util.*;
public class GFG {
// To count the no. of digits in a number
static void count_digits(int n)
{
// converting number to string using
// to_string in C++
String num = Integer.toString(n);
// calculate the size of string
System.out.println(+num.length());
}
// Driver code
public static void main(String args[])
{
// number
int n = 345;
count_digits(n);
}
}
// Code is contributed by shivanisinghss2110
Python3
# Python3 implementation of the approach
def count_digits(n):
n = str(n)
return len(n)
# Driver code
n = 456533457776
print(count_digits(n))
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG {
// To count the no. of digits in a number
static void count_digits(int n)
{
// converting number to string using
// to_string in C#
string num = Convert.ToString(n);
// calculate the size of string
Console.WriteLine(+num.Length);
}
// Driver Code
public static void Main(string[] args)
{
// number
int n = 345;
count_digits(n);
}
}
// This code is contributed by shivanisinghss2110
输出
3