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 Number = 24689131 Given another Number (k) = 2
Output:
The sum of digits at odd places of the given number{ 24689131 } is divisible by the another given number k{ 2 }
Example2:
Input:
Given Number = 12573 Given another Number (k) = 5
Output:
The sum of digits at odd places of the given number{ 12573 } is not divisible by the another given number k{ 5 }
Program to Check Whether Sum of digits at Odd places of a Number is Divisible by K in Python
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.
Method #1: Using For Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Give the other number k as static input and store it in another variable.
- Convert the given number to a string using the str() function and store it in another variable.
- Create a list of digits say “digtslst” using map(),list(),int functions.
- Store it in another variable.
- Take another variable say “od_sum” and initialize it with 0.
- Loop in the above list of digits until the length of the “digtslst” using the for loop.
- Check if the iterator value is odd or not using the if conditional statement.
- If the statement is true, then add the element of digits list at the iterator value to the “od_sum” and store it in the same variable od_sum.
- Check if the od_sum modulus given number k is equal to 0 or not using the if conditional statement.
- If the statement is true, then print “The sum of digits at odd places of the given number is divisible by the another given number k.
- If the statement is false, then print “The sum of digits at odd places of the given number is Not divisible by the another given number k.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 12573 # Give the other number k as static input and store it in another variable. gvn_k = 5 # Convert the given number to a string using the str() function and store it in # another variable. stringnum = str(gvn_numb) # Create a list of digits say "digtslst" using map(),list(),int functions. digtslst = list(map(int, stringnum)) # Take a variable say "od_sum" and initialize it with 0. od_sum = 0 # Loop in the above list of digits until the length of the "digtslst" using the for loop. for itr in range(len(digtslst)): # Check if the iterator value is odd or not using # the if conditional statement. if(itr % 2 != 0): # If the statement is true, then add the element of digits list at iterator value to # the "od_sum" and store it in the same variable od_sum. od_sum += digtslst[itr] # Check if the od_sum modulus given number k is equal to 0 or not using the if conditional # statement. if(od_sum % gvn_k == 0): # If the statement is true, then print "The sum of digits at odd places of the given # number is divisible by the another given number k. print("The sum of digits at odd places of the given number{", gvn_numb, "} is divisible by the another given number k{", gvn_k, "}") else: # If the statement is false, then print "The sum of digits at odd places of the given # number is Not divisible by the another given number k. print("The sum of digits at odd places of the given number{", gvn_numb, "} is not divisible by the another given number k{", gvn_k, "}")
Output:
The sum of digits at odd places of the given number{ 12573 } is not divisible by the another given number k{ 5 }
Method #2: Using For loop (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Give the other number k as user input using the int(input()) function and store it in another variable.
- Convert the given number to a string using the str() function and store it in another variable.
- Create a list of digits say “digtslst” using map(),list(),int functions.
- Store it in another variable.
- Take another variable say “od_sum” and initialize it with 0.
- Loop in the above list of digits until the length of the “digtslst” using the for loop.
- Check if the iterator value is odd or not using the if conditional statement.
- If the statement is true, then add the element of digits list at the iterator value to the “od_sum” and store it in the same variable od_sum.
- Check if the od_sum modulus given number k is equal to 0 or not using the if conditional statement.
- If the statement is true, then print “The sum of digits at odd places of the given number is divisible by the another given number k.
- If the statement is false, then print “The sum of digits at odd places of the given number is Not divisible by the another given number k.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. gvn_numb = int(input("Enter some random number = ")) #Give the other number k as user input using the int(input()) function and store it in another variable. gvn_k = int(input("Enter some random number = ")) # Convert the given number to a string using the str() function and store it in # another variable. stringnum = str(gvn_numb) # Create a list of digits say "digtslst" using map(),list(),int functions. digtslst = list(map(int, stringnum)) # Take a variable say "od_sum" and initialize it with 0. od_sum = 0 # Loop in the above list of digits until the length of the "digtslst" using the for loop. for itr in range(len(digtslst)): # Check if the iterator value is odd or not using # the if conditional statement. if(itr % 2 != 0): # If the statement is true, then add the element of digits list at iterator value to # the "od_sum" and store it in the same variable od_sum. od_sum += digtslst[itr] # Check if the od_sum modulus given number k is equal to 0 or not using the if conditional # statement. if(od_sum % gvn_k == 0): # If the statement is true, then print "The sum of digits at odd places of the given # number is divisible by the another given number k. print("The sum of digits at odd places of the given number{", gvn_numb, "} is divisible by the another given number k{", gvn_k, "}") else: # If the statement is false, then print "The sum of digits at odd places of the given # number is Not divisible by the another given number k. print("The sum of digits at odd places of the given number{", gvn_numb, "} is not divisible by the another given number k{", gvn_k, "}")
Output:
Enter some random number = 624351 Enter some random number = 3 The sum of digits at odd places of the given number{ 624351 } is divisible by the another given number k{ 3 }
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
- Python Program to Check Whether Product of Digits at Even places of a Number is Divisible by K
- Python Program to Check if Product of Digits of a Number at Even and Odd places is Equal
- Python Program to Find the Sum of Digits of a Number at Even and Odd places
- Python Program to Count the Number of Odd and Even Digits