{"id":7312,"date":"2023-11-01T10:19:12","date_gmt":"2023-11-01T04:49:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=7312"},"modified":"2023-11-10T12:11:14","modified_gmt":"2023-11-10T06:41:14","slug":"python-program-to-count-the-number-of-digits-present-in-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-the-number-of-digits-present-in-a-number\/","title":{"rendered":"Python Program to Count the Number of Digits Present In a Number"},"content":{"rendered":"
Count the number of numbers in a number using Python. We learn how to count the total number of digits in a number using python in this tutorial. The program receives the number and prints the total number of digits in the given number. We’ll show you three ways to calculate total numbers in a number.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to count the number of digits present in a number some of them are:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n The idea behind this approach is to keep eliminating the number’s rightmost digit one by one until the number reaches zero. The following algorithm will be used for this approach:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n We begin by converting the number value to a string using str (). The length of the string is then determined using len ().<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This is similar to method #2 Count the number of numbers in a number using Python. We learn how to count the total number of digits in a number using python in this tutorial. The program receives the number and prints the total number of digits in the given number. We’ll show you three ways to calculate total numbers in a …<\/p>\nnumber = 27482<\/pre>\n
The total digits present in the given number= 5<\/pre>\n
number = 327<\/pre>\n
The total digits present in the given number= 3<\/pre>\n
Program to Count the Number of Digits Present In a Number in Python<\/h2>\n
\n
Method #1:Using while loop<\/h3>\n
\n
# given number\r\ngiven_number = 27482\r\n# initializing a variable that counts the digit of the given number.\r\n# initlaize it with 0\r\ndigits_count = 0\r\n# using while loop to traverse digit by digit of the given number\r\nwhile (given_number > 0):\r\n # divide the number by 10\r\n given_number = given_number\/\/10\r\n # increment the count\r\n digits_count = digits_count + 1\r\n# printing the digits count of the given number\r\nprint(\"The total digits present in the given number=\", digits_count)\r\n<\/pre>\n
The total digits present in the given number= 5<\/pre>\n
\n
Method #2:Using string length function<\/h3>\n
# given number\r\ngiven_number = 27482\r\n# converting the given_number to string using str() function\r\nstrnum = str(given_number)\r\n# finding the digits of the given_number using len() function\r\ndigits_count = len(strnum)\r\n# printing the digits count of the given number\r\nprint(\"The total digits present in the given number=\", digits_count)\r\n<\/pre>\n
The total digits present in the given number= 5<\/pre>\n
Method #3:Converting number to list and calculating length of list<\/h3>\n
\n
# given number\r\ngiven_number = 27482\r\n# converting the given_number to list of digits using list() function\r\nnumberslist = list(str(given_number))\r\n# finding the digits of the given_number using len() function\r\ndigits_count = len(numberslist)\r\n# printing the digits count of the given number\r\nprint(\"The total digits present in the given number=\", digits_count)\r\n<\/pre>\n
The total digits present in the given number= 5<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n