Question

python If a number num1 divides another number num2 evenly then num1 is a divisor of...

python

If a number num1 divides another number num2 evenly then num1 is a divisor of num2. For example, 2 is a divisor of 2, 4, 6, 8, but 2 is not a divisor of 1, 3, 5, 7, 9, 11, 13.

Write a function named count_divisors(m,n) that works as follows.

  1. Input: the function takes two integer arguments m and n
  2. Process: the function asks the user to enter numbers (positive or negative), and counts the total number of inputs that are divisible by either m or n, but not by both. Store this total number in a variable called count. This process stops if the user enters the string ‘stop’. We assume that user always enters valid input (either an integer or 'stop').
  3. Output: finally the function returns count.    

Example 1:

# We call the function with m = 2, n = 3

count = count_divisors(2, 3)

user input -> 1, 6, 5, -4, 7, 9, 12, stop. count = 2

user input -> -6, 2, 9, 16, 8, -5, stop. count = 4


Example 2:

# We call the function with m = 5, n = 2

count = count_divisors(5, 2)

user input -> 1, 6, 5, -4, 7, 9, 12, stop. count = 4

user input -> -6, 2, 10, 16, 8, -5, stop. count = 5

Homework Answers

Answer #1

Program Code Screenshot



Program Sample Console Input/Output Screenshot



Program Code to Copy

def count_divisor(m, n):
    count = 0
    nums = input().split(", ")
    for num in nums:
        if(num == "stop"):
            return count
        num = int(num)
        if(num % m == 0 or num % n == 0):
            count += 1


print("count = ", count_divisor(2, 3))
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. ( Java programing...
Subject- ( App Development for Web) ( language C#, software -visual studio) Exact question is-Firstly the...
Subject- ( App Development for Web) ( language C#, software -visual studio) Exact question is-Firstly the console calculator is created which perform multiply,divide,sub and add, operation and it accept all type of data (divide by 0 case as well ).Now the main motive is to create the library project from the console calculator project .Than we have to create a unit test project which test the functionality of added library.Make test like Test 1. multiply two positive number,Test 2. Add...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...
Be able to write Python programming of a factorial of an integer (given input parameters, output...
Be able to write Python programming of a factorial of an integer (given input parameters, output characteristics and expected output behavior, do not use internal python functions for factorials), recall for example that: 5! = 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 = 120 and to continue asking unless user says to stop NOT USING FACTORIAL FUNCTION
PLEASE EXPLAIN THE ANSWER The file data1 contains: 0 3 6 9 12 The file data2...
PLEASE EXPLAIN THE ANSWER The file data1 contains: 0 3 6 9 12 The file data2 contains: 10 8 6 4 2 The file data3 contains: 20 10 0 20 80 Give the output when run with: ./a.out data1 data2 data3 #include <stdio.h> int main( int argc, char *argv[] ) { FILE *fpOne = fopen(argv[1], "r"); FILE *fpTwo = fopen(argv[2], "r"); FILE *fpThree = fopen(argv[3], "r"); int a, num1, num2, num3, sum1=0, sum2=0; for (a=0; a<5; a++) { fscanf(fpOne, "%d",...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
A Harshad number (or a Niven number) is a number that is evenly divisible by the...
A Harshad number (or a Niven number) is a number that is evenly divisible by the sum of its digits. An example is 18 (1+8=9, 18%9 = 0). Write a function called isHarshad(num) that takes an integer as an argument and returns True if the number is a Harshad number and False if it is not. Then, use this function to create a list of all of the Harshad numbers in the first 500 natural numbers.
Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features...
Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features the design of a calculator program using pseudocode and a Python program that uses a list and functions with variable scope to add, subtract, multiply, and divide. Open the M4Lab1ii.py program in IDLE and save it as M4Lab1ii.py with your initials instead of ii. Each lab asks you to write pseudocode that plans the program’s logic before you write the program in Python and...
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.