{"id":18519,"date":"2021-08-26T10:38:55","date_gmt":"2021-08-26T05:08:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=18519"},"modified":"2021-11-22T18:37:17","modified_gmt":"2021-11-22T13:07:17","slug":"python-program-to-check-whether-sum-of-digits-at-odd-places-of-a-number-is-divisible-by-k","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-whether-sum-of-digits-at-odd-places-of-a-number-is-divisible-by-k\/","title":{"rendered":"Python Program to Check Whether Sum of digits at Odd places of a Number is Divisible by K"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Even Digits Sum and Odd Digits Sum Divisible by 4 and 3 Respectively<\/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 digits at odd places of a given number is divisible by the another given input number say K.<\/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 Even Digits Sum and Odd Digits Sum Divisible by 4 and 3 Respectively The task is to check if the sum of digits at odd places of a given number is divisible by the another given input number say K. Examples: Example1: Input: Given …<\/p>\n
\nThe task is to check if the sum of digits at odd places of a given number is divisible by the another given input number say K.<\/p>\nGiven Number = 24689131\r\nGiven another Number (k) = 2<\/pre>\n
The sum of digits at odd places of the given number{ 24689131 } is divisible by the another given number k{ 2 }<\/pre>\n
Given Number = 12573\r\nGiven another Number (k) = 5<\/pre>\n
The sum of digits at odd places of the given number{ 12573 } is not divisible by the another given number k{ 5 }<\/pre>\n
Program to Check Whether Sum of digits at Odd places of a Number is Divisible by K 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\ngvn_numb = 12573\r\n# Give the other number k as static input and store it in another variable.\r\ngvn_k = 5\r\n# Convert the given number to a string using the str() function and store it in\r\n# another variable.\r\nstringnum = str(gvn_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 \"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 iterator value is odd or not using\r\n # the if conditional statement.\r\n if(itr % 2 != 0):\r\n # If the statement is true, then add the element of digits list at iterator value to\r\n # the \"od_sum\" and store it in the same variable od_sum.\r\n od_sum += digtslst[itr]\r\n# Check if the od_sum modulus given number k is equal to 0 or not using the if conditional\r\n# statement.\r\nif(od_sum % gvn_k == 0):\r\n # If the statement is true, then print \"The sum of digits at odd places of the given\r\n # number is divisible by the another given number k.\r\n print(\"The sum of digits at odd places of the given number{\", gvn_numb,\r\n \"} is divisible by the another given number k{\", gvn_k, \"}\")\r\nelse:\r\n # If the statement is false, then print \"The sum of digits at odd places of the given\r\n # number is Not divisible by the another given number k.\r\n print(\"The sum of digits at odd places of the given number{\", gvn_numb,\r\n \"} is not divisible by the another given number k{\", gvn_k, \"}\")\r\n<\/pre>\n
The sum of digits at odd places of the given number{ 12573 } is not divisible by the another given number k{ 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 store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n#Give the other number k as user input using the int(input()) function and store it in another variable.\r\ngvn_k = int(input(\"Enter some random number = \"))\r\n# Convert the given number to a string using the str() function and store it in\r\n# another variable.\r\nstringnum = str(gvn_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 \"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 iterator value is odd or not using\r\n # the if conditional statement.\r\n if(itr % 2 != 0):\r\n # If the statement is true, then add the element of digits list at iterator value to\r\n # the \"od_sum\" and store it in the same variable od_sum.\r\n od_sum += digtslst[itr]\r\n# Check if the od_sum modulus given number k is equal to 0 or not using the if conditional\r\n# statement.\r\nif(od_sum % gvn_k == 0):\r\n # If the statement is true, then print \"The sum of digits at odd places of the given\r\n # number is divisible by the another given number k.\r\n print(\"The sum of digits at odd places of the given number{\", gvn_numb,\r\n \"} is divisible by the another given number k{\", gvn_k, \"}\")\r\nelse:\r\n # If the statement is false, then print \"The sum of digits at odd places of the given\r\n # number is Not divisible by the another given number k.\r\n print(\"The sum of digits at odd places of the given number{\", gvn_numb,\r\n \"} is not divisible by the another given number k{\", gvn_k, \"}\")\r\n<\/pre>\n
Enter some random number = 624351\r\nEnter some random number = 3\r\nThe sum of digits at odd places of the given number{ 624351 } is divisible by the another given number k{ 3 }<\/pre>\n
\n