Help Alex, a new programmer, create a straightforward program. Ask for a positive number, use a function named isDuckNumber() to check if it's a duck number (having at least one zero, not as the first digit), and print the result in the main function.This tool aids Alex in spotting duck numbers, enhancing their programming journey.
Question
Help Alex, a new programmer, create a straightforward program. Ask for a positive number, use a function named isDuckNumber() to check if it's a duck number (having at least one zero, not as the first digit), and print the result in the main function.This tool aids Alex in spotting duck numbers, enhancing their programming journey.
Solution
Sure, here is a simple Python program that does what you asked for:
def isDuckNumber(n):
n = list(str(n))
if (n[0] == '0'):
return False
else:
for i in range(1, len(n)):
if (n[i] == '0'):
return True
return False
def main():
num = int(input("Enter a positive number: "))
if (num > 0):
if (isDuckNumber(num)):
print(num, "is a Duck Number")
else:
print(num, "is not a Duck Number")
else:
print("Please enter a positive number")
main()
This program works as follows:
- The
mainfunction asks the user to input a positive number. - If the number is positive, it calls the
isDuckNumberfunction with the number as an argument. - The
isDuckNumberfunction converts the number to a string and checks each digit. If it finds a '0' that is not the first digit, it returnsTrue, otherwise it returnsFalse. - Back in the
mainfunction, it checks the return value ofisDuckNumber. If it'sTrue, it prints that the number is a Duck Number. If it'sFalse, it prints that the number is not a Duck Number. - If the number entered by the user is not positive, it prints a message asking for a positive number.
Similar Questions
Single File Programming QuestionProblem StatementHelp Alex, a new programmer, create a straightforward program. Ask for a positive number, use a function named isDuckNumber() to check if it's a duck number (having at least one zero, not as the first digit), and print the result in the main function.This tool aids Alex in spotting duck numbers, enhancing their programming journey.Note: This question helps in clearing technical coding tests for companies like Amazon and Adobe.Input format :The input consists of an integer n, representing the number to be checked.Output format :The output prints "The number is a duck number." if the entered number is a duck number or "The number is NOT a duck number." otherwise.Refer to the sample output for formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ n ≤ 1000Sample test cases :Input 1 :1Output 1 :The number is NOT a duck number.Input 2 :330Output 2 :The number is a duck number.Input 3 :012Output 3 :The number is NOT a duck number.Input 4 :1000Output 4 :The number is a duck number
Write a program to check whether a number is positive or negative. Take an integer, num, as an input from the console using input() function and print the result to the console as shown in the sample test case. Sample Input and Output 1: num: 02 positive Sample Input and Output 2: num: -52142 negative Sample Input and Output 3: num: 0 zero
Given an integer ‘n’, Develop an algorithm and write a java program to check whether it is a Narcissistic number or not.Note: Narcissistic Number is a number that is the sum of its own digits each raised to the power of the number of digits.Boundary Condition: 0 < n <= 10000Otherwise, print ‘invalid’ Input FormatNumber ‘n’Output Formatprint a single line containing the string "yes" if the presented scenario is possible or "no" otherwise (without quotes).
Write a program numbers.cpp that defines a functionbool isDivisibleBy(int n, int d);If n is divisible by d, the function should return true, otherwise return false.For example:isDivisibleBy(100, 25) == trueisDivisibleBy(35, 17) == falseThe program should also have a main function that tests your code. For example, it can ask the user to input two integer numbers and print Yes if the first number is divisible by the second, otherwise print No.
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.
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.