### 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.

• Problem description:

 Write a Java program that reads in an integer n... and prints all its divisors

• A concrete example:

 Input: n = 12 Output: 1, 2, 3, 4, 6, 12

• What would you do to solve this problem ?

• Problem:

 Find all divisor of the number 12

• 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

• 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

• Reminder: Notation

• The notation:

 for (x = 1, 2, 3, ...., 10) do { print x; }

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

• This notation can be converted to a for-statement very naturally:

 for ( x = 1; x <= 10; x = x + 1 ) { print x; }

• 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 !) }

Structure diagram of the algorithm:

Notice it is much easier to write the algorithm using a for-statement (than a while-statement - in this example)

• 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; 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) } } } }

• Example Program: (Demo above code)

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

• A concrete example:

 Input: n = 5 Output: 15   (because 1 + 2 + 3 + 4 + 5 = 15)

• 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

• 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;

Sample execution (n = 5):

 sum = 0; ---> sum = 0 for-statement: add 1 to sum; ---> sum = 1 add 2 to sum; ---> sum = 3 add 3 to sum; ---> sum = 6 add 4 to sum; ---> sum = 10 add 5 to sum; ---> sum = 15 Print sum; ---> Prints 15

• Statement to perform the task: "add x to sum"

 sum = sum + x;

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 !

Structure diagram of the algorithm:

• 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; 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 } }

• Example Program: (Demo above code)

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

• A concrete example:

 Input: n = 5 Output: 120   (because 1 × 2 × 3 × 4 × 5 = 120)

• 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) 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

• 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;

Sample execution (n = 5):

 product = 1; ---> product = 1 for-statement: multiply 1 into product; ---> product = 1 multiply 2 into product; ---> product = 2 multiply 3 into product; ---> product = 6 multiply 4 into product; ---> product = 24 multiply 5 into product; ---> product = 120 Print product; ---> Prints 120

• Statement to perform the task: "multiply x into product"

 product = product * x;

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 !

Structure diagram of the algorithm:

• 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; 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 } }

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Factorial01.java To run:          java Factorial01