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=" ")     ___
Using a “for loop” print all even numbers in range from 1 to 1000. Also, please...
Using a “for loop” print all even numbers in range from 1 to 1000. Also, please count and print how many even numbers you have found.
Summary In this lab the completed program should print the numbers 0 through 10, along with...
Summary In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10. Execute the program. Is...
(Write in PythonUse a for statement to print 10 random numbers between 25 and 35, inclusive....
(Write in PythonUse a for statement to print 10 random numbers between 25 and 35, inclusive. (Write in Python)in python  The Pythagorean Theorem tells us that the length of the hypotenuse of a right triangle is related to the lengths of the other two sides. Look through the math module and see if you can find a function that will compute this relationship for you. Once you find it, write a short program to try it out. (Write in PythonSearch on...
Write a program to print random numbers and whether they were even or odd. Ask the...
Write a program to print random numbers and whether they were even or odd. Ask the user to enter a positive number to indicate how many random numbers to pull (verify number is positive). Zero is positive. Declare two variables and initialized to zero. int evens = 0, odds = 0; Use them to count how many evens and how many odds were pulled. Enter how many random numbers to pull : 3 41           odd 18467    odd 6334    even 1...
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
(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...
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....
Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers....
Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers. Randomly pick a number between one and ten and then sequentially search the array for that number. Once the number is found, place that number at the top of the list. Print the list. Perform #3 thru #5 ten times.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT