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.
Examples:
Example1:
Input:
Given Number = 123452
Output:
yes, the even digits sum and odd digits sum of a given number{ 123452 } is divisible by 4 and 3 respectively.
Example2:
Input:
Given Number = 4532176
Output:
No, the even digits sum and odd digits sum of a given number{ 4532176 } is not divisible by 4 and 3 respectively.
- Python Program to Find nth Prime Number
- Python Program to Check whether all Digits of a Number Divide it
- Python Program to Print Neon numbers in a Range
Program to Find Even Digits Sum and Odd Digits Sum Divisible by 4 and 3 Respectively in Python:
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.
Method #1: Using For Loop (Static Input)
Approach:
- Give the number as static input and store it in a 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 a variable say “evn_sum” and initialize it with 0.
- 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 element of the “digtslst” is even or not using the if conditional statement.
- If the statement is true, then add the element of the “digtslst” to the “evn_sum” and store it in the same variable evn_sum.
- If the statement is false, then add the element of the “digtslst” to the “od_sum ” and store it in the same variable od_sum.
- Check if the evn_sum modulus 4 is equal to 0 and od_sum modulus 3 is equal to 0 using the if conditional statement.
- If the statement is true, print “yes, the even digits sum and odd digits sum of a given number are divisible by 4 and 3 respectively.
- If the statement is false, print “No, the even digits sum and odd digits sum of a given number are not divisible by 4 and 3 respectively.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. numb = 123452 # Convert the given number to string using the str() function. stringnum = str(numb) # Create a list of digits say "digtslst" using map(),list(),int functions. digtslst = list(map(int, stringnum)) # Take a variable say "evn_sum" and initialize it with 0. evn_sum = 0 # Take another 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 element of the "digtslst" is even or not using the if conditional statement. if(digtslst[itr] % 2 == 0): # If the statement is true, then add the element of the "digtslst" to the "evn_sum" # and store it in the same variable evn_sum. evn_sum += digtslst[itr] else: # If the statement is false, then add the element of the "digtslst" to the "od_sum" # and store it in the same variable od_sum. od_sum += digtslst[itr] # Check if the evn_sum modulus 4 is equal to 0 and od_sum modulus 3 is equal to 0 # using the if conditional statement. if(evn_sum % 4 == 0 and od_sum % 3 == 0): # If the statement is true, print "yes, the even digits sum and odd digits sum of a # given number are divisible by 4 and 3 respectively. print( "yes, the even digits sum and odd digits sum of a given number{", numb, "} is divisible by 4 and 3 respectively.") else: # If the statement is false, print "No, the even digits sum and odd digits sum # of a given number are not divisible by 4 and 3 respectively. print( "No, the even digits sum and odd digits sum of a given number{", numb, "} is not divisible by 4 and 3 respectively.")
Output:
yes, the even digits sum and odd digits sum of a given number{ 123452 } is divisible by 4 and 3 respectively.
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.
- 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 a variable say “evn_sum” and initialize it with 0.
- 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 element of the “digtslst” is even or not using the if conditional statement.
- If the statement is true, then add the element of the “digtslst” to the “evn_sum” and store it in the same variable evn_sum.
- If the statement is false, then add the element of the “digtslst” to the “od_sum ” and store it in the same variable od_sum.
- Check if the evn_sum modulus 4 is equal to 0 and od_sum modulus 3 is equal to 0 using the if conditional statement.
- If the statement is true, print “yes, the even digits sum and odd digits sum of a given number are divisible by 4 and 3 respectively.
- If the statement is false, print “No, the even digits sum and odd digits sum of a given number are not divisible by 4 and 3 respectively.
- 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. numb = int(input("Enter some random number = ")) # Convert the given number to string using the str() function. stringnum = str(numb) # Create a list of digits say "digtslst" using map(),list(),int functions. digtslst = list(map(int, stringnum)) # Take a variable say "evn_sum" and initialize it with 0. evn_sum = 0 # Take another 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 element of the "digtslst" is even or not using the if conditional statement. if(digtslst[itr] % 2 == 0): # If the statement is true, then add the element of the "digtslst" to the "evn_sum" # and store it in the same variable evn_sum. evn_sum += digtslst[itr] else: # If the statement is false, then add the element of the "digtslst" to the "od_sum" # and store it in the same variable od_sum. od_sum += digtslst[itr] # Check if the evn_sum modulus 4 is equal to 0 and od_sum modulus 3 is equal to 0 # using the if conditional statement. if(evn_sum % 4 == 0 and od_sum % 3 == 0): # If the statement is true, print "yes, the even digits sum and odd digits sum of a # given number are divisible by 4 and 3 respectively. print( "yes, the even digits sum and odd digits sum of a given number{", numb, "} is divisible by 4 and 3 respectively.") else: # If the statement is false, print "No, the even digits sum and odd digits sum # of a given number are not divisible by 4 and 3 respectively. print( "No, the even digits sum and odd digits sum of a given number{", numb, "} is not divisible by 4 and 3 respectively.")
Output:
Enter some random number = 4532176 No, the even digits sum and odd digits sum of a given number{ 4532176 } is not divisible by 4 and 3 respectively.
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 Sum of digits at Odd places of a Number is Divisible by K
- 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