Writing algorithms using the for-statement Programming example 1: find all divisors of a number • We have seen a program using a while-statement to solve this problem. Here, we will see that this problem can be solved more naturally using a for-statement. Programming example 1: find all divisors of a number (cont.) • Problem description: • Write a Java program that reads in an integer n... • and prints all its divisors Programming example 1: find all divisors of a number (cont.) • A concrete example: • Input: n = 12 • Output: 1, 2, 3, 4, 6, 12 Programming example 1: find all divisors of a number (cont.) • What would you do to solve this problem ? • Problem: • Find all divisor of the number 12 Programming example 1: find all divisors of a number (cont.) • I think you would have done this: • Check if 12 is divisible by 1 • Check if 12 is divisible by 2 • ... • Check if 12 is divisible by 12 Programming example 1: find all divisors of a number (cont.) • Note: • We do not need to check numbers > 12 because only number ≤ 12 can be divisors ! • When the remainder of the division is equal to 0, we print out the divisor Programming example 1: find all divisors of a number (cont.) • Reminder: Notation • The notation: for (x = 1, 2, 3, ...., 10) do { print x; } Programming example 1: find all divisors of a number (cont.) means: • The variable x takes on (assumes, gets) the value 1, 2, 3, ..., 10, one at a time • For each individual value taken on by x, you perform the operation "print x" once Programming example 1: find all divisors of a number (cont.) • This notation can be converted to a for-statement very naturally: for ( x = 1; x <= 10; x = x + 1 ) { print x; } Programming example 1: find all divisors of a number (cont.) • Rough algorithm (pseudo code) to find all divisors: input: n = some integer number for (x = 1, 2, 3, ..., n) do { if ( n is divisible by x ) then print x; (because x is a divisor !) } Programming example 1: find all divisors of a number (cont.) • Structure diagram of the algorithm: Notice it is much easier to write the algorithm using a forstatement (than a while-statement - in this example) Programming example 1: find all divisors of a number (cont.) • Java program: import java.util.Scanner; public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int x; Programming example 1: find all divisors of a number (cont.) System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number for ( x = 1; x <= n; x++ ) // Run x = 1, 2, ..., n { if ( n % x == 0 ) { // x is a divisor of n System.out.println(x); // Print x (because it's a divisor) } } } } Programming example 1: find all divisors of a number (cont.) • Example Program: (Demo above code) – Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/ Divisors03.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Divisors03.java • To run: java Divisors03 Programming example 2: compute the sum 1+2+3+...+n • Problem description: • Write a Java program that reads in an integer n... • and prints the sum 1+2+3+...+n Programming example 2: compute the sum 1+2+3+...+n (cont.) • A concrete example: • Input: n = 5 • Output: 15 (because 1 + 2 + 3 + 4 + 5 = 15) Programming example 2: compute the sum 1+2+3+...+n (cont.) • What would you do to solve this problem ? • Imagine again that you are using a calculator.... • I think you would have done this: • Punch clear to set the total to 0. • Press 1, then press add to add to the running total • Press 2, then press add to add to the running total • ... • Press 5, then press add to add to the running total • Show the total Programming example 2: compute the sum 1+2+3+...+n (cont.) • Rough algorithm (pseudo code) to compute 1 + 2 + 3 + ... + n: input: n = some integer number sum = 0; // Clear the running total ! for ( x = 1, 2, 3, ..., n ) do { Add x to sum } Print sum; Programming example 2: compute the sum 1+2+3+...+n (cont.) • Sample execution (n = 5): sum = 0; for-statement: add 1 to sum; add 2 to sum; add 3 to sum; add 4 to sum; add 5 to sum; Print sum; ---> sum = 0 ---> sum = 1 ---> sum = 3 ---> sum = 6 ---> sum = 10 ---> sum = 15 ---> Prints 15 Programming example 2: compute the sum 1+2+3+...+n (cont.) • Statement to perform the task: "add x to sum" sum = sum + x; Programming example 2: compute the sum 1+2+3+...+n (cont.) • Example: Suppose the variable sum contains 6 and x = 4 Then: sum = sum + x; = 6 + 4; = 10; ---> Result, sum = 10 We have added 4 to sum ! Programming example 2: compute the sum 1+2+3+...+n (cont.) • Structure diagram of the algorithm: Programming example 2: compute the sum 1+2+3+...+n (cont.) • Java program: import java.util.Scanner; public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int x, sum; Programming example 2: compute the sum 1+2+3+...+n (cont.) System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number sum = 0; // Clear running total for ( x = 1; x <= n; x++ ) // Run x = 1, 2, ..., n { sum = sum + x; // Add x to sum } System.out.println(sum); // Print final sum } } Programming example 2: compute the sum 1+2+3+...+n (cont.) • Example Program: (Demo above code) – Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/ RunningSum01.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac RunningSum01.java • To run: java RunningSum01 Programming example 3: compute factorial n! • Problem description: • Write a Java program that reads in an integer n... • and prints the factorial n! = 1×2×3×...×n Programming example 3: compute factorial n! (cont.) • A concrete example: • Input: n = 5 • Output: 120 (because 1 × 2 × 3 × 4 × 5 = 120) Programming example 3: compute factorial n! (cont.) • We can re-use the previous programming paradigm to construct the algorithm: • Imagine you are using a calculator.... • This is how one may compute the factorial: Punch 1 to set the running product to 1. (You can't use 0 as starting value for multiplication , because the multiplying with 0 will always produce 0) Programming example 3: compute factorial n! (cont.) • Press 1, then press multiply to multiply 1 into the running product • Press 2, then press multiply to multiply 2 into the running product • ... • Press 5, then press multiply to multiply 5 into the running product • Show the product Programming example 3: compute factorial n! (cont.) • Rough algorithm (pseudo code) to compute n! = 1 × 2 × 3 × ... × n: input: n = some integer number product = 1; // Set running product to 1 for ( x = 1, 2, 3, ..., n ) do { Multiply x into product } Print sum; Programming example 3: compute factorial n! (cont.) • Sample execution (n = 5): product = 1; ---> product = 1 for-statement: multiply 1 into product; multiply 2 into product; multiply 3 into product; multiply 4 into product; multiply 5 into product; Print product; ---> product = 1 ---> product = 2 ---> product = 6 ---> product = 24 ---> product = 120 ---> Prints 120 Programming example 3: compute factorial n! (cont.) • Statement to perform the task: "multiply x into product" product = product * x; Programming example 3: compute factorial n! (cont.) • Example: Suppose the variable product contains 6 and x = 4 Then: product = product * x; = 6 * 4; = 24; ---> Result, product = 24 We have multiplied 4 into the product ! Programming example 3: compute factorial n! (cont.) • Structure diagram of the algorithm: Programming example 3: compute factorial n! (cont.) • Java program: import java.util.Scanner; public class Factorial01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int x, product; Programming example 3: compute factorial n! (cont.) System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number product = 1; // Set init. product to 1 for ( x = 1; x <= n; x++ ) // Run x = 1, 2, ..., n { product = product + x; // Multiply x into product } System.out.println(product); // Print final product } } Programming example 3: compute factorial n! (cont.) • Example Program: (Demo above code) – Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/ Factorial01.java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Factorial01.java • To run: java Factorial01