banner



How To Find Length Of Integer In Java

Program to count digits in an integer (4 Different Methods)

Count the number of digits in a long integer entered by a user.

Elementary Iterative Solution
The integer entered by the user is stored in the variable due north. Then the while loop is iterated until the test expression n != 0 is evaluated to 0 (false).

  1. Later the first iteration, the value of n will be 345 and the count is incremented to 1.
  2. After the 2d iteration, the value of n will exist 34 and the count is incremented to two.
  3. Later the third iteration, the value of n will be 3 and the count is incremented to 3.
  4. At the commencement of the fourth iteration, the value of due north will exist 0 and the loop is terminated.

Then the examination expression is evaluated for false and the loop terminates.

C++

#include <bits/stdc++.h>

using namespace std;

int countDigit( long long northward)

{

int count = 0;

while (northward != 0)

{

n = n / 10;

++count;

}

return count;

}

int primary( void )

{

long long n = 345289467;

cout << "Number of digits : " << countDigit(n);

render 0;

}

C

#include <stdio.h>

int countDigit( long long n)

{

int count = 0;

while (due north != 0)

{

northward = due north / ten;

++count;

}

render count;

}

int master( void )

{

long long n = 345289467;

printf ( "Number of digits : %d" , countDigit(n));

return 0;

}

Java

class GFG {

static int countDigit( long due north)

{

int count = 0 ;

while (north != 0 ) {

north = due north / 10 ;

++count;

}

return count;

}

public static void master(String[] args)

{

long n = 345289467 ;

Organization.out.print( "Number of digits : "

+ countDigit(north));

}

}

Python3

def countDigit(n):

count = 0

while due north ! = 0 :

n / / = 10

count + = one

return count

n = 345289467

impress ( "Number of digits : % d" % (countDigit(n)))

C#

using Organization;

class GFG {

static int countDigit( long n)

{

int count = 0;

while (n != 0)

{

north = n / 10;

++count;

}

return count;

}

public static void Principal()

{

long n = 345289467;

Console.WriteLine( "Number of"

+ " digits : " + countDigit(n));

}

}

PHP

<?php

office countDigit( $n )

{

$count = 0;

while ( $n != 0)

{

$north = round ( $n / 10);

++ $count ;

}

return $count ;

}

$n = 345289467;

echo "Number of digits : "

. countDigit( $n );

?>

Javascript

<script>

function countDigit(n)

{

allow count = 0;

while (n != 0)

{

due north = Math.floor(n / 10);

++count;

}

return count;

}

north = 345289467;

certificate.write( "Number of digits : " + countDigit(n));

</script>

Output

Number of digits : 9

Recursive Solution:

C++

#include <bits/stdc++.h>

using namespace std;

int countDigit( long long n)

{

if (n/x == 0)

return 1;

return 1 + countDigit(n / x);

}

int principal( void )

{

long long north = 345289467;

cout << "Number of digits :" << countDigit(n);

return 0;

}

C

#include <stdio.h>

int countDigit( long long n)

{

if (n/10 == 0)

return 1;

return one + countDigit(north / 10);

}

int master( void )

{

long long n = 345289467;

printf ( "Number of digits : %d" , countDigit(northward));

return 0;

}

Coffee

import java.util.*;

course GFG {

static int countDigit( long n)

{

if (n/ ten == 0 )

return 1 ;

return 1 + countDigit(n / ten );

}

public static void main(Cord[] args)

{

long n = 345289467 ;

Organization.out.print( "Number of digits : "

+ countDigit(n));

}

}

Python3

def countDigit(north):

if north / 10 = = 0 :

render 1

return ane + countDigit(n / / 10 )

n = 345289467

print ( "Number of digits : % d" % (countDigit(n)))

C#

using System;

class GFG {

static int countDigit( long n)

{

if (n/10 == 0)

return one;

return 1 + countDigit(due north / 10);

}

public static void Main()

{

long due north = 345289467;

Panel.WriteLine( "Number of "

+ "digits : "

+ countDigit(n));

}

}

PHP

<?php

part countDigit( $n )

{

if ( $due north /ten == 0)

return 1;

return 1 + countDigit((int)( $n / 10));

}

$due north = 345289467;

print ( "Number of digits : " .

(countDigit( $n )));

?>

Javascript

<script>

function countDigit(northward)

{

if (n/10 == 0)

render i;

return 1 + countDigit(parseInt(n / 10));

}

var n = 345289467;

document.write( "Number of digits :" + countDigit(north));

</script>

Output

Number of digits :9

Log-based Solution:
We can employ log10(logarithm of base of operations ten) to count the number of digits of positive numbers (logarithm is not defined for negative numbers).
Digit count of N = upper bound of log10(N).

C++

#include <bits/stdc++.h>

using namespace std;

int countDigit( long long n) {

render floor ( log10 (n) + 1);

}

int main( void )

{

long long n = 345289467;

cout << "Number of digits : "

<< countDigit(n);

return 0;

}

C

#include <math.h>

#include <stdio.h>

int countDigit( long long n) {

return floor ( log10 (n) + 1);

}

int main( void )

{

long long n = 345289467;

printf ( "Number of digits : %d" , countDigit(n));

return 0;

}

Java

import java.util.*;

course GFG {

static int countDigit( long north)

{

render ( int )Math.floor(Math.log10(n) + 1 );

}

public static void main(Cord[] args)

{

long north = 345289467 ;

System.out.print( "Number of digits : "

+ countDigit(north));

}

}

Python3

import math

def countDigit(northward):

return math.floor(math.log10(n) + 1 )

north = 345289467

print ( "Number of digits : % d" % (countDigit(north)))

C#

using Arrangement;

grade GFG {

static int countDigit( long n)

{

return ( int )Math.Floor(Math.Log10(n) + ane);

}

public static void Main()

{

long n = 345289467;

Console.WriteLine( "Number of digits : "

+ countDigit(n));

}

}

PHP

<?php

function countDigit( $northward )

{

return floor (log10( $n )+1);

}

$n = 345289467;

echo "Number of digits : " ,

countDigit( $n );

?>

Javascript

<script>

function countDigit(north)

{

return Math.floor(Math.log10(north) + 1);

}

var north = 345289467;

document.write( "Number of digits : " +

countDigit(n));

</script>

Output

Number of digits : 9

Method 4:
We can convert the number into a string and so find the length of the cord to get the number of digits in the original number.

C++

#include <$.25/stdc++.h>

using namespace std;

void count_digits( int north)

{

cord num = to_string(n);

cout << num.size() << endl;

}

int main()

{

int north = 345;

count_digits(n);

return 0;

}

Coffee

import coffee.util.*;

public class GFG {

static void count_digits( int north)

{

Cord num = Integer.toString(n);

Organisation.out.println(+num.length());

}

public static void main(String args[])

{

int northward = 345 ;

count_digits(north);

}

}

Python3

def count_digits(n):

n = str (n)

return len (north)

n = 456533457776

print (count_digits(n))

C#

using System;

using Arrangement.Collections.Generic;

course GFG {

static void count_digits( int due north)

{

cord num = Convert.ToString(due north);

Console.WriteLine(+num.Length);

}

public static void Main( cord [] args)

{

int northward = 345;

count_digits(n);

}

}

Javascript

<script>

office count_digits(n)

{

let num = n.toString();

certificate.write(num.length);

}

permit north = 345;

count_digits(n);

</script>

This article is contributed by Vishal Kumar Gupta. If you lot similar GeeksforGeeks and would like to contribute, you can also write an commodity using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. Run into your article appearing on the GeeksforGeeks primary page and assist other Geeks.
Please write comments if you observe anything incorrect, or you want to share more information most the topic discussed in a higher place.

Attention reader! Don't finish learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Class at a student-friendly price. To complete your training from learning a language to DS Algo and many more than,  delight refer Complete Interview Grooming Course .


Source: https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods/

Posted by: pattersonparienve.blogspot.com

0 Response to "How To Find Length Of Integer In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel