Posts

Showing posts with the label count the number of digits in python

Python program to count the number of digits in a number

Image
Count the number of digits in a number Problem description : The program gets a number as input from the user and returns the number of digits in the number. For example, number = 845 Here for the above number the number of digits is equal to 3. Sample input : n = 1324 Sample output : Number of digits = 4 Method 1 : In this method,  We are using a while loop until the number becomes zero.   "//" operator is used , it d ivides the number on its left by the number on its right, rounds down the answer, and returns a whole number. For example,  45 // 10 = 4 762 // 10 = 76 After performing the division operation the count variable is increased by one. Procedure : 1. Start. 2. Get the number as input from user and store it in a variable. 3. Initialize digit count variable as 0. 4. Loop (until n = 0)     => increment digit count     => num = num // 10 5. Print digit count. 6. End.  Code : #Get the number as input from the user and store it in a variable  n = int(input("En