HomePythonPython Program to Count Number of Digits in an Integer

Python Program to Count Number of Digits in an Integer

In this python tutorial, you will learn how to Count Number of Digits in an Integer using the while loop, inbuilt methods and the assignment operators of the python programming language.

How to Count Number of Digits in an Integer?

Let’s take a look at the first source code , here the values are given as input by the user in the code, the assignment operators along with the while loop carry out the function.

RUN CODE SNIPPET
#Python Program to Count Number of Digits in an Integer using while loop

num =int(input("Enter the integer: ")) 
count = 0

while num != 0:
    num //= 10
    count += 1

print("\nNumber of digits: " + str(count))

INPUT:

979203580462

OUTPUT:

Enter the integer: 
Number of digits: 12
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable num with the statements/strings ("Enter the integer: ")  , we use the int  function and declare the  input value as an integer value.
  2. We assign the variable count with the integer 0.
  3. Next, we declare a while loop with the condition num != 0 followed by a :, after which we declare the condition num //= 10 where count += 1 .
  4. In this code, the while loop is iterated until the test expression num != 0 is equal to 0. During each iteration, num will be divided and the count is incremented by one. When the test expression is evaluated to false and the loop terminates.
  5. In the STDIN section of the code editor the input values are entered.
  6. The final output values are displayed with the statement ("\nNumber of digits: " + str(count)), using the print function, where the + str(count) returns and displays the string version of the count value.

Let’s take a look at the second source code , here the values are given as input by the user in the code, inbuilt methods and print statement carry out the function.

RUN CODE SNIPPET
#Python Program to Count Number of Digits in an Integer using inbuilt methods

num =int(input("Enter the integer: "))
print("\nNumber of digits:",(len(str(num))))

INPUT:

979203580462

OUTPUT:

Enter the integer: 
Number of digits: 12
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable num with the statements/strings ("Enter the integer: ")  , we use the int  function and declare the  input value as an integer value.
  2. In the STDIN section of the code editor the input values are entered.
  3. The print function will display the statement ("\nNumber of digits:",(len(str(num)))), where the function (len(str(num)))will display the length of the value in string format which the variable num holds .

NOTE:

  • The != operator is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal.
  • The += operator know as the addition assignment operator lets you add two values together and assign the resultant value to a variable.
  • The //= operator know as the double division assignment operator lets you divide two values together and assign the resultant value to a variable.
  • The str() function in python returns the string version of the object.
  • The While true loop in python runs without any conditions until the break statement executes inside the loop.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The len() function is an inbuilt function in Python programming language that returns the length of the string.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement/string to be displayed in enclosed in double quotes.
  • The input() function allows a user to insert a value into a program, it returns a integer value.
  • The statement for the input function are enclosed in single quotes and parenthesis.

Leave a Reply

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...