{"id":18543,"date":"2021-08-26T10:38:29","date_gmt":"2021-08-26T05:08:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=18543"},"modified":"2021-11-22T18:37:18","modified_gmt":"2021-11-22T13:07:18","slug":"python-program-to-find-even-digits-sum-and-odd-digits-sum-divisible-by-4-and-3-respectively","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-even-digits-sum-and-odd-digits-sum-divisible-by-4-and-3-respectively\/","title":{"rendered":"Python Program to Find Even Digits Sum and Odd Digits Sum Divisible by 4 and 3 Respectively"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the 2nd Largest Digit in a Given Number<\/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 check if the sum of even digits of a number is divisible by 4 and the sum of odd digits of a given number is divisible by 3.<\/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 Find the 2nd Largest Digit in a Given Number Given a number, and the task is to check if the sum of even digits of a number is divisible by 4 and the sum of odd digits of a given number is divisible by 3. …<\/p>\n
\nGiven a number, and the task is to check if the sum of even digits of a number is divisible by 4 and the sum of odd digits of a given number is divisible by 3.<\/p>\nGiven Number = 123452<\/pre>\n
yes, the even digits sum and odd digits sum of a given number{ 123452 } is divisible by 4 and 3 respectively.<\/pre>\n
Given Number = 4532176<\/pre>\n
No, the even digits sum and odd digits sum of a given number{ 4532176 } is not divisible by 4 and 3 respectively.<\/pre>\n
Program to Find Even Digits Sum and Odd Digits Sum Divisible by 4 and 3 Respectively 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 = 123452\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_sum\" and initialize it with 0.\r\nevn_sum = 0\r\n# Take another variable say \"od_sum\" and initialize it with 0.\r\nod_sum = 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 element of the \"digtslst\" is even or not using the if conditional statement.\r\n if(digtslst[itr] % 2 == 0):\r\n # If the statement is true, then add the element of the \"digtslst\" to the \"evn_sum\"\r\n # and store it in the same variable evn_sum.\r\n evn_sum += digtslst[itr]\r\n else:\r\n # If the statement is false, then add the element of the \"digtslst\" to the \"od_sum\"\r\n # and store it in the same variable od_sum.\r\n od_sum += digtslst[itr]\r\n# Check if the evn_sum modulus 4 is equal to 0 and od_sum modulus 3 is equal to 0\r\n# using the if conditional statement.\r\nif(evn_sum % 4 == 0 and od_sum % 3 == 0):\r\n # If the statement is true, print \"yes, the even digits sum and odd digits sum of a\r\n # given number are\u00a0divisible by 4 and 3 respectively.\r\n print(\r\n \"yes, the even digits sum and odd digits sum of a given number{\", numb, \"} is divisible by 4 and 3 respectively.\")\r\nelse:\r\n # If the statement is false, print \"No, the even digits sum and odd digits sum\r\n # of a given number are not divisible by 4 and 3 respectively.\r\n print(\r\n \"No, the even digits sum and odd digits sum of a given number{\", numb, \"} is not divisible by 4 and 3 respectively.\")\r\n<\/pre>\n
yes, the even digits sum and odd digits sum of a given number{ 123452 } is divisible by 4 and 3 respectively.<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and 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_sum\" and initialize it with 0.\r\nevn_sum = 0\r\n# Take another variable say \"od_sum\" and initialize it with 0.\r\nod_sum = 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 element of the \"digtslst\" is even or not using the if conditional statement.\r\n if(digtslst[itr] % 2 == 0):\r\n # If the statement is true, then add the element of the \"digtslst\" to the \"evn_sum\"\r\n # and store it in the same variable evn_sum.\r\n evn_sum += digtslst[itr]\r\n else:\r\n # If the statement is false, then add the element of the \"digtslst\" to the \"od_sum\"\r\n # and store it in the same variable od_sum.\r\n od_sum += digtslst[itr]\r\n# Check if the evn_sum modulus 4 is equal to 0 and od_sum modulus 3 is equal to 0\r\n# using the if conditional statement.\r\nif(evn_sum % 4 == 0 and od_sum % 3 == 0):\r\n # If the statement is true, print \"yes, the even digits sum and odd digits sum of a\r\n # given number are\u00a0divisible by 4 and 3 respectively.\r\n print(\r\n \"yes, the even digits sum and odd digits sum of a given number{\", numb, \"} is divisible by 4 and 3 respectively.\")\r\nelse:\r\n # If the statement is false, print \"No, the even digits sum and odd digits sum\r\n # of a given number are not divisible by 4 and 3 respectively.\r\n print(\r\n \"No, the even digits sum and odd digits sum of a given number{\", numb, \"} is not divisible by 4 and 3 respectively.\")\r\n<\/pre>\n
Enter some random number = 4532176\r\nNo, the even digits sum and odd digits sum of a given number{ 4532176 } is not divisible by 4 and 3 respectively.<\/pre>\n
\n