Question

In: Computer Science

Instructions:  Code a for statement that increments from 1 through 10, but print only the even numbers...

Instructions:  Code a for statement that increments from 1 through 10, but print only the even numbers using a fall-thru switch. You can catch the odd numbers using the option in the switch that handles everything else. Name your program ForFallThruSwitch.java. Use Java Style Guide in line advancing and spacing.

--------------------OUTPUT RESULTS--------------------

Not printing odd numbers!

2 is an even number.

Not printing odd numbers!

4 is an even number.

Not printing odd numbers!

6 is an even number.

Not printing odd numbers!

8 is an even number.

Not printing odd numbers!

10 is an even number.

//class header

//Begin class scope.

        //main method header

        //Begin method scope.

              //for header using i as loop-control variable

              //Begin for scope.

           

                    //Code switch expression.

                    //Begin switch scope.

                       

                          //First case.

                          //Next statement.

                          //Next statement.

                          //Next statement.

                          //Next statement.

                                //Print the number and that it is even, e.g., “2 is an even number.”

                          //Next statement.

                          //Code option that handles the odd numbers.

                                 //Print "Not printing odd numbers!”

                    //Close switch scope.

              //Close for scope.

              //Exit program.

        //End method scope.

//End class scope.

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
import java.io.*;
class ForFallThruSwitch
{
        public static void main (String[] args) 
        {
        for(int i=1;i<=10;i++)
        {
            //fall through switch
            switch(i){
                //handles all even cases
                case 2: 
                case 4: 
                case 6: 
                case 8: 
                case 10: System.out.println(i+" is an even number."); 
                         System.out.println();
                         break;
                        //handles all odd cases
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 9: System.out.println("Not printing odd numbers!"); 
                                System.out.println();
                        break;
                }
            }
        }
    }

Output:-


Related Solutions

Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding...
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding in NASM on a SASM system.
Fill in the blanks of this code to print out the numbers 1 through 7. number...
Fill in the blanks of this code to print out the numbers 1 through 7. number = 1 while number ___ 7:     print(number, end=" ")     ___
C++ Create the code Read  numbers from stdin and print their sum to stdout. Input Format One...
C++ Create the code Read  numbers from stdin and print their sum to stdout. Input Format One line that contains  space-separated integers:a , b, c and . Constraints 1≤ a,b,c ≤1000 Output Format Print the sum of the three numbers on a single line. Sample Input 1 2 7 Sample Output 10 Explanation The sum of the three numbers is 1+2+7=10
Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print...
Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product. Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6. Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10, write the word blue. If the number is between 10 and 20, write the word red....
(MATLAB Question) 1a. Establish a variable “x” that goes from -10 to 10 in increments of...
(MATLAB Question) 1a. Establish a variable “x” that goes from -10 to 10 in increments of .1 1b. Create an equation of a line (y=mx+b) based on “x” with slope m=2 and y-intercept 2. (y=2x+2) 1c. Plot the line (plot(x,y)). 1d. Turn on a plot hold (hold on). 1e. Create a second equation of a line (y=mx+b) based on “x” with negative slope, slope m=-2 and a negative y-intercept -2. (y=-2x-2) 1f. Plot the second line (plot (x,y)) 1g. Create...
Consider the following code segment:    count = 1    while count <= 10:       print(count,...
Consider the following code segment:    count = 1    while count <= 10:       print(count, end=" ") Which of the following describes the error in this code? The loop control variable is not properly initialized. The comparison points the wrong way. The loop is infinite. The loop is off by 1. Does this code contain an error? If so, what line is the error on? 0: ans = input("Yes/No? ") 1: if ans == "Yes": 2: print("Confirmed!") 3: else...
Write assembly instructions that compute the following: The sum of all even numbers between 2 and...
Write assembly instructions that compute the following: The sum of all even numbers between 2 and 100 (inclusive) -- the answer should be 2550 Prompt the user for 2 values, put them in variables a and b, then find the sum of all odd numbers between a and b. The sum of all the squares between 1 and 100 (inclusive) . -- the answer should be 338350 All of the statements above should print the answers using print_int MUST BE...
I am to write a code that uses two queues, and print the generated random numbers...
I am to write a code that uses two queues, and print the generated random numbers and content of both queues. I need to use the enqueue and dequeue functions in the code.  The instructions are below. Program should be in C Instructions: Generate n random numbers with values between 10 - 100. Note: n>9 if u write a function (for example, called generateRand) to do this - what is the data or input we have to give it? Create a...
Write a c++ code that prompts the user to enter three float numbers and print them...
Write a c++ code that prompts the user to enter three float numbers and print them in acsending order
Create a for loop that will print numbers 57, 21, 99, 10, 80
Create a for loop that will print numbers 57, 21, 99, 10, 80
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT