{"id":18505,"date":"2021-08-26T10:38:58","date_gmt":"2021-08-26T05:08:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=18505"},"modified":"2021-11-22T18:37:17","modified_gmt":"2021-11-22T13:07:17","slug":"python-program-to-count-the-number-of-odd-and-even-digits","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-the-number-of-odd-and-even-digits\/","title":{"rendered":"Python Program to Count the Number of Odd and Even Digits"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Replace a Word with Asterisks in a Sentence<\/a> 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 Below are the ways to count the number of odd and even digits in a given number :<\/p>\n Approach:<\/strong><\/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 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 In the previous article, we have discussed Python Program to Replace a Word with Asterisks in a Sentence Given a number and the task is to count the number of odd and even digits in a given number. Examples: Example1: Input: Given Number = 1237891 Output: The count of even digits in a given number{ …<\/p>\n
\nGiven a number and the task is to count the number of odd and even digits in a given number.<\/p>\nGiven Number = 1237891<\/pre>\n
The count of even digits in a given number{ 1237891 } = 2\r\nThe count of odd digits in a given number{ 1237891 } = 5<\/pre>\n
Given Number = 78342186453<\/pre>\n
The count of even digits in a given number{ 78342186453 } = 6\r\nThe count of odd digits in a given number{ 78342186453 } = 5<\/pre>\n
Program to Count the Number of Odd and Even Digits in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\nnumb = 1237891\r\n# Convert the given number to string using the str() function.\r\nstringnum = str(numb)\r\n# Create a list of digits say \"digtslst\" using map(),list(),int functions.\r\ndigtslst = list(map(int, stringnum))\r\n# Take a variable say \"evn_count\" and initialize it with 0.\r\nevn_count = 0\r\n# Take another variable say \"od_count\" and initialize it with 0.\r\nod_count = 0\r\n# Loop in the above list of digits until the length of the \"digtslst\" using the for loop.\r\nfor itr in range(len(digtslst)):\r\n # Check if the iterator value of \"digtslst\" is even or not using\r\n # the if conditional statement.\r\n if(digtslst[itr] % 2 == 0):\r\n # If the statement is true, increment the count value of\u00a0\"evn_count\" by 1.\r\n evn_count += 1\r\n else:\r\n # If the statement is false,\u00a0increment the count value of \"od_count\" by 1.\r\n od_count += 1\r\n# Print \"evn_count\" to get the count of even digits in a given number.\r\nprint(\"The count of even digits in a given number{\", numb, \"} =\", evn_count)\r\n# Print \"od_count\" to get the count of odd digits in a given number.\r\nprint(\"The count of odd digits in a given number{\", numb, \"} =\", od_count)\r\n<\/pre>\n
The count of even digits in a given number{ 1237891 } = 2\r\nThe count of odd digits in a given number{ 1237891 } = 5<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and\r\n# store it in a variable.\r\nnumb = int(input(\"Enter some random number = \"))\r\n# Convert the given number to string using the str() function.\r\nstringnum = str(numb)\r\n# Create a list of digits say \"digtslst\" using map(),list(),int functions.\r\ndigtslst = list(map(int, stringnum))\r\n# Take a variable say \"evn_count\" and initialize it with 0.\r\nevn_count = 0\r\n# Take another variable say \"od_count\" and initialize it with 0.\r\nod_count = 0\r\n# Loop in the above list of digits until the length of the \"digtslst\" using the for loop.\r\nfor itr in range(len(digtslst)):\r\n # Check if the iterator value of \"digtslst\" is even or not using\r\n # the if conditional statement.\r\n if(digtslst[itr] % 2 == 0):\r\n # If the statement is true, increment the count value of\u00a0\"evn_count\" by 1.\r\n evn_count += 1\r\n else:\r\n # If the statement is false,\u00a0increment the count value of \"od_count\" by 1.\r\n od_count += 1\r\n# Print \"evn_count\" to get the count of even digits in a given number.\r\nprint(\"The count of even digits in a given number{\", numb, \"} =\", evn_count)\r\n# Print \"od_count\" to get the count of odd digits in a given number.\r\nprint(\"The count of odd digits in a given number{\", numb, \"} =\", od_count)\r\n<\/pre>\n
Enter some random number = 78342186453\r\nThe count of even digits in a given number{ 78342186453 } = 6\r\nThe count of odd digits in a given number{ 78342186453 } = 5<\/pre>\n
\n