Question

In: Computer Science

PYTHON: Using a counter initialized to 0, count how many even numbers are multiples of 3...

PYTHON:

Using a counter initialized to 0, count how many even numbers are multiples of 3 in a range of numbers 1 to 300. If the counter reaches 20, break out of the loop and stop counting.

Solutions

Expert Solution

The problem can be analyzed first so that it will be easier to implement the Python code.

1. Using a counter, initialized to 0, the even numbers which are multiple of 3 has to be counted in the range of numbers from 1 to 300. This can be implemented using a for loop. The loop will run for loop variable 1 to 300 and check if the number or loop variable is divisible by both 2 (even number) and 3 (multiples of 3). If the number is divisible then increment the counter.
2. If the counter reaches 20, break out of the loop and stop counting. This can be implemented by using break statement. This statement stops the loop from further execution.
3. Print necessary details to confirm that the counter has reached 20.


The required PYTHON code is given below:

counter=0         #initializing counter variable
for x in range(1,301):        #loop to check numbers from 1 to 300
if(x%2==0 and x%3==0):      #checking if the number is even & multiple of 3
    counter+=1                #if the condition satisfies then increment the count
    if(counter==20):          #checking if counter reaches 20
      break                   #if counter reaches 20 then break the loop and stops the counting
print("Current Counter =",counter)      #printing the counter
print("Last counted number =",x)      #displaying the last value counted by the counter
print("Counter has reached 20")

Screenshot of the code:

Output:

NOTE: To ensure correct parentheses, check the screenshot of the code. Also, the checking of the "counter==20" line comes under the above " if " statement, so that after updation of the counter, the variable can be checked at that moment.


Related Solutions

Design a counter with an external control, x, to count the sequence of multiples of 3...
Design a counter with an external control, x, to count the sequence of multiples of 3 (i.e., 0-3-6 and repeat) when the control is 0, and non-multiples of 3 (i.e., 1-2-4-5-7 and repeat) when the control is 1. These values will be displayed on a seven-segment display. When the control value changes, the first clock should drive the output to the first value in the appropriate count- e.g., if the circuit has been counting non multiples and the control switches...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits 0 to 9 occur in all numbers between a and b, inclusive. In order to make your code simpler, implement first the function intToList(n) that takes as input one integer n, and returns a list with the digits of n in the order they appear. For example, intToList(1964) should return [1,9,6,4]. Using the function intToList, implement the function digitsCount(a, b) that returns a list...
Using the programing language of Verilog I attempted to make a counter to count from 0...
Using the programing language of Verilog I attempted to make a counter to count from 0 to 9 then loop back to 0 using the internal clock of the FPGA cyclone IV from altera. the code is posted at the bottom I get the following errors Error (10663): Verilog HDL Port Connection error at Encryption.v(9): output or inout port "clk" must be connected to a structural net expression Error (10285): Verilog HDL Module Instantiation error at Encryption.v(9): instance "" specifies...
How many valid 3 digit numbers can you make using the digits 0, 1, 2 and...
How many valid 3 digit numbers can you make using the digits 0, 1, 2 and 3 without repeating the digits? How about with repeating?
In python, read the file credit_cards.txt into a dictionary with the count of how many cards...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file. credit_cards.txt contains the following data: John Smith, Discover Helen Jones, Visa Jerry Jones, Master Card Julio Jones, Diners Club Fred Jones, Diners Club Anthony Rendon, Platinum Visa Juan Soto, Platinum Visa George Jones, American Express Brandon Allen, Visa Henry Beureguard, Visa Allen Jackson, Master Card Faith Hill, Platinum Visa David Smith, Master Card Samual Jackson,...
a) How many 3-digit numbers are there? b) How many 3-digit numbers can you make with...
a) How many 3-digit numbers are there? b) How many 3-digit numbers can you make with all three digits different? c) How many of the numbers is part b) are odd?
For a 3 digit code with distinct numbers. (0-9) How many combinations to get the code...
For a 3 digit code with distinct numbers. (0-9) How many combinations to get the code (max)? How many combinations if you remember the middle number is 1?
How many even 4-digit numbers can be formed from the digits 1, 2, 3, 6, and...
How many even 4-digit numbers can be formed from the digits 1, 2, 3, 6, and 9 with no repetitions allowed? (Hint: Try filling the units place first.)
How many 4 digit numbers can be formed using only the numbers 3 2 7 6...
How many 4 digit numbers can be formed using only the numbers 3 2 7 6 5 4 9 if:    a) there are no repeats allowed? b) the number is odd and no repeats allowed? c) the number is greater than 4000 and no repeats are allowed? d) the number is greater than 4000 and repeats are allowed?
a. How many 3-digit numbers can you make using the digits from the set of (3,...
a. How many 3-digit numbers can you make using the digits from the set of (3, 4, 5, 6, 8, 2, 7) without repetitions? b. How many numbers can be made with the digits from the set of (0, 3, 5, 7, 8, 4, 9), which are greater than 0 and less than a thousand (repetitions are allowed)?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT