计算整数位数的程序(4种不同的方法)

计算用户输入的长整数的位数。

null

图片[1]-计算整数位数的程序(4种不同的方法)-yiteyi-C++库

简单迭代解 用户输入的整数存储在变量n中。然后while循环被迭代,直到测试表达式n!=0被计算为0(false)。

  1. 在第一次迭代之后,n的值将是345,计数将增加到1。
  2. 第二次迭代后,n的值将为34,计数将增加到2。
  3. 在第三次迭代之后,n的值将是3,计数将增加到3。
  4. 在第四次迭代开始时,n的值将为0,循环终止。

然后对测试表达式求值为false,循环终止。

C++

// Iterative C++ program to count
// number of digits in a number
#include <bits/stdc++.h>
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 <stdio.h>
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

<?php
// Iterative PHP program to count
// number of digits in a number
function countDigit( $n )
{
$count = 0;
while ( $n != 0)
{
$n = round ( $n / 10);
++ $count ;
}
return $count ;
}
// Driver code
$n = 345289467;
echo "Number of digits : "
. countDigit( $n );
//This code is contributed by mits
?>


Javascript

<script>
// Iterative Javascript program to count
// number of digits in a number
function countDigit(n)
{
let count = 0;
while (n != 0)
{
n = Math.floor(n / 10);
++count;
}
return count;
}
// Driver code
n = 345289467;
document.write( "Number of digits : " + countDigit(n));
// This code is contributed by Mayank Tyagi
</script>


输出

Number of digits : 9

递归解决方案:

C++

// Recursive C++ program to count number of
// digits in a number
#include <bits/stdc++.h>
using namespace std;
int countDigit( long long n)
{
if (n/10 == 0)
return 1;
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 <stdio.h>
int countDigit( long long n)
{
if (n/10 == 0)
return 1;
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/ 10 == 0 )
return 1 ;
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 / 10 = = 0 :
return 1
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/10 == 0)
return 1;
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

<?php
// Recursive PHP program to count
// number of digits in a number
function countDigit( $n )
{
if ( $n /10 == 0)
return 1;
return 1 + countDigit((int)( $n / 10));
}
// Driver Code
$n = 345289467;
print ( "Number of digits : " .
(countDigit( $n )));
// This code is contributed by mits
?>


Javascript

<script>
// Recursive Javascript program to count number of
// digits in a number
function countDigit(n)
{
if (n/10 == 0)
return 1;
return 1 + countDigit(parseInt(n / 10));
}
// Driver code
var n = 345289467;
document.write( "Number of digits :" + countDigit(n));
</script>


输出

Number of digits :9

基于日志的解决方案: 我们可以使用log10(以10为底的对数)来计算正数的位数(负数不定义对数)。 数字计数 N=log10(N)的上限 .

C++

// Log based C++ program to count number of
// digits in a number
#include <bits/stdc++.h>
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 <math.h>
#include <stdio.h>
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

<?php
// Log based php program to
// count number of digits
// in a number
function countDigit( $n )
{
return floor (log10( $n )+1);
}
// Driver code
$n = 345289467;
echo "Number of digits : " ,
countDigit( $n );
// This code is contributed by ajit
?>


Javascript

<script>
// Log based Javascript program to count number of
// digits in a number
function countDigit(n)
{
return Math.floor(Math.log10(n) + 1);
}
// Driver code
var n = 345289467;
document.write( "Number of digits : " +
countDigit(n));
// This code is contributed by noob2000
</script>


输出

Number of digits : 9

方法4: 我们可以把数字转换成一个字符串,然后找到字符串的长度,得到原始数字的位数。

C++

#include <bits/stdc++.h>
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


Javascript

<script>
// Javascript implementation of the above approach
// To count the no. of digits in a number
function count_digits(n)
{
// converting number to string using
// to_string in javascript
let num = n.toString();
// calculate the size of string
document.write(num.length);
}
// number
let n = 345;
count_digits(n);
</script>


输出

3

本文由 维沙尔·库马尔·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享