Switch, Do-while & For-loops

  1. Write a block of code that includes a switch statement that is equivalent to the following code.
char letterGrade = sc.next().charAt(0);
double maxGrade;
if (letterGrade == 'A')
    maxGrade = 100;
else if (letterGrade == 'B')
    maxGrade = 90;
else
    maxGrade = 80;
  1. Write a block of code that includes a switch statement that is equivalent to the following code.
int count = sc.nextInt();
double discount;
if (count == 200)
    discount = 10.00;
else if (count == 100)
    discount = 5.00;
else
    discount = 0;
  1. Suppose a variable named letterGrade has been declared and initialized to one of the characters A, B, C, D, or F. Write a block of code that uses a switch-statement to set a variable named points to a value dependent on the value of letterGrade according to the following table.

letterGrade points
A 5
B 4
C 3
D 2
F 0

  1. Write a block of code that utilizes a do-while loop to print to the console the numbers 0 to 100 backwards.
  2. Write a block of code that utilizes a do-while loop to sums all of the integers between 0 and 100 that have a 6 in the 1’s digit.
  3. Write a block of code that utilizes a for-loop to print to the console the even numbers between 0 and 100 whose digits sum to 7.
  4. Write a block of code that utilizes a for-loop to sums all of the integers between 0 and 100 that are prime.

© 2017, Eric. All rights reserved.