Digit SeparationA Competition is scheduled in Buddy4Study Primary School so that the KG students can get some appreciation in understanding odd and even numbers. The task is to identify the even digits which are both preceded and followed by an odd digit. As the number of student registrations is high, your help in this regard is highly expected. Develop a python program for generating the output as shown in sample input and output. The first and last digits cannot be included in output, as they donot have preceding digit and following digit, respectively. If no such digit is found, print "NIL".Sample Input:56474182729Sample Output: (Display output digits, each in a separate line)42
Question
Digit SeparationA Competition is scheduled in Buddy4Study Primary School so that the KG students can get some appreciation in understanding odd and even numbers. The task is to identify the even digits which are both preceded and followed by an odd digit. As the number of student registrations is high, your help in this regard is highly expected. Develop a python program for generating the output as shown in sample input and output. The first and last digits cannot be included in output, as they donot have preceding digit and following digit, respectively. If no such digit is found, print "NIL".Sample Input:56474182729Sample Output: (Display output digits, each in a separate line)42
Solution
Sure, I can help you with that. Here is a step-by-step guide to solving the problem:
- Start by defining a function called
find_even_digitsthat takes a number as input. - Convert the number to a string using the
str()function so that we can iterate over its digits. - Initialize an empty list called
output_digitsto store the even digits that meet the given condition. - Iterate over each digit in the string representation of the number, excluding the first and last digits.
- Check if the current digit is even and both the preceding and following digits are odd.
- If the condition is met, append the current digit to the
output_digitslist. - After iterating over all the digits, check if any even digits were found.
- If the
output_digitslist is not empty, print each digit on a separate line. - If the
output_digitslist is empty, print "NIL".
Here is the Python code that implements the above steps:
def find_even_digits(number):
number_str = str(number)
output_digits = []
for i in range(1, len(number_str) - 1):
digit = int(number_str[i])
preceding_digit = int(number_str[i - 1])
following_digit = int(number_str[i + 1])
if digit % 2 == 0 and preceding_digit % 2 != 0 and following_digit % 2 != 0:
output_digits.append(digit)
if output_digits:
for digit in output_digits:
print(digit)
else:
print("NIL")
# Test the function with the given sample input
find_even_digits(56474182729)
When you run this code, it will produce the following output:
4
2
I hope this helps! Let me know if you have any further questions.
Similar Questions
Write a program to print the odd values in the given rangeInput FormatTwo space separated integers - 'start' and 'end'Output FormatDisplay all the odd numbers between 'start' and 'end'Sample Input10 20Sample Output11 13 15 17 19
Write a program to print only the even numbers till given NInput FormatA single integer 'N'Output FormatDisplay all the even numbers between 0 and NSample Input10Sample Output0 2 4 6 8 10
Rohit is tasked with designing a program to analyze the digits of a given integer. Write a program to help Rohit that takes an integer as input and identifies the minimum odd digit and the maximum even digit present in the number. If no odd or even digits are present, display appropriate messages. Implement the solution using a 'while' loop to iterate through the digits of the given number.Input format :The input consists of an integer n.Output format :The first line of output prints the minimum odd digit in the given integer.If there is no odd digit, print "There are no odd digits in the number."The second line prints the maximum even digit in the given integer.If there is no even digit, print "There are no even digits in the number."Refer to the sample output for formatting specifications.Code constraints :The given test cases fall under the following constraints:1 ≤ n ≤ 107Sample test cases :Input 1 :3465Output 1 :Minimum odd digit: 3Maximum even digit: 6Input 2 :97975Output 2 :Minimum odd digit: 5There are no even digits in the number.Input 3 :24642Output 3 :There are no odd digits in the number.Maximum even digit: 6
Study the following arrangement carefully and answer the question given below:4 2 6 1 3 8 9 5 8 1 6 5 1 5 8 5 3 9 4 9 2 3 2 1 5 7 2 6 2 4 2 7 4How many such 2’s are there in the above arrangement each of which is immediately preceded by an odd digit and also immediately followed by an odd digit?
If two is subtracted from each odd digit and three is added to each even digit in the number 94257636, how many digits will appear twice in the new number thus formed ?Options1023
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.