-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProduct1ToN.java
More file actions
38 lines (34 loc) · 1.01 KB
/
Product1ToN.java
File metadata and controls
38 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Write a program called Product1ToN to compute the product of integers
* 1 to 10 (i.e., 1×2×3×...×10).
* Try computing the product from 1 to 11, 1 to 12, 1 to 13 and 1 to 14.
* Write down the product obtained and explain the results.
*
* Hints: Declares an int variable called product (to accumulate the product)
* and initialize to 1.
*/
package javaexercises.flowcontrol;
/**
*
* @author User
*/
public class Product1ToN {
public static void main(String[] args)
{
Product1ToN aProduct1ToN = new Product1ToN();
aProduct1ToN.printProduct1ToN(10);
aProduct1ToN.printProduct1ToN(11);
aProduct1ToN.printProduct1ToN(12);
aProduct1ToN.printProduct1ToN(13);
aProduct1ToN.printProduct1ToN(14);
}
private void printProduct1ToN(int n)
{
long prod = 1L;
for(int i=1; i <= n; i++) {
prod *= i;
}
System.out.println("8. Product of integers 1 to " + n + ".");
System.out.println(prod);
}
}