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,...
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
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?
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT