Which form of loop seems to work best Why public class DoWhileLoop public

Which form of loop seems to work best why public

This preview shows page 4 - 8 out of 15 pages.

Which form of loop seems to work best? Why? public class DoWhileLoop { public static void main(String[] args ) { int total = 0; int i = 1; do { total += i ; i ++; } while ( i <=100 ); System. out .println( total ); } } Lab Manual Chapter 6 3/27/18 4
Image of page 4
4) You can test to see if an integer, x, is even or odd using the Boolean expression (x / 2) * 2 == x . Integers that are even make this expression true, and odd integers make the expression false. Use a for loop to iterate five times. In each iteration, request an integer from the user. Print each integer the user types, and whether it is even or odd. Keep up with the number of even and odd integers the user types, and print “Done” when finished, so the user won’t try to type another integer. Finally, print out the number of even and odd integers that were entered. import java.util.Scanner; public class ForLoops { public static void main(String[] args ) { Scanner in = new Scanner(System. in ); int odd = 0; int even = 0; for ( int count =1; count <=5; count ++) { System. out .print( "Enter an integer: " ); int x = in .nextInt(); if (( x / 2) * 2 == x ) { System. out .println( x + " is even" ); } else { System. out .println( x + " is odd" ); } } in .close(); System. out .println( "Done" ); } } COMPUTER SCIENCE MAJORS 5*) One of the oldest numerical algorithms was described by the Greek mathematician, Euclid, in 300 B.C. That algorithm is described in Book VII of Euclid’s multi-volume work Elements. It is a simple but very effective algorithm that computes the greatest common divisor of two given integers. For instance, given integers 24 and 18, the greatest common divisor is 6, because 6 is the largest integer that divides evenly into both 24 and 18. We will denote the greatest common divisor of x and y as gcd(x, y). The algorithm is based on the clever idea that the gcd(x, y) = gcd(x – y, y) if x >= y. The algorithm consists of a series of steps (loop iterations) where the “larger” integer is replaced by the difference of the larger and smaller integer. Lab Manual Chapter 6 3/27/18 5
Image of page 5
In the example below, we compute gcd(72, 54) and list each loop iteration computation on a separate line. The whole process stops when one of the integers becomes zero. When this happens, the greatest common divisor is the non-zero integer. gcd(72, 54) = gcd(72 – 54, 54) = gcd(18, 54) gcd(18, 54) = gcd(18, 54 – 18) = gcd(18, 36) gcd(18, 36) = gcd(18, 36 – 18) = gcd(18, 18) gcd(18, 18) = gcd(18 – 18, 18) = gcd(0, 18) = 18 To summarize: Create a loop, and subtract the smaller integer from the larger one (if the integers are equal you may choose either one as the “larger”) during each iteration. Replace the larger integer with the computed difference. Continue looping until one of the integers becomes zero. Print out the non-zero integer. Use the code below to prompt the user for the two integers. public class GCD { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter the first integer: "); int x = in.nextInt(); System.out.println("x = " + x); System.out.println("Enter the second integer: "); int y = in.nextInt(); System.out.println("y = " + y); // Your gcd computation code goes here } } Lab Manual Chapter 6 3/27/18 6
Image of page 6
6.1) Use nested for loops to produce the following output
Image of page 7
Image of page 8

You've reached the end of your free preview.

Want to read all 15 pages?

  • Fall '12
  • DuiWei
  • Lab manual chapter

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture