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.
For Hints visit Hints on Exercise SumAndAverage (Loop)
Hints: Declares an int variable called product (to accumulate the product) and initialize to 1.
For Hints visit Hints on Exercise SumAndAverage (Loop)
ReplyDeletepublic class Product1ToN {
public static void main(String[] args) {
int lowerBound = 1;
int upperBound = 10;
int product = 1;
int product1 = 1;
int product2 = 1;
int product3 = 1;
int product4 = 1;
for (lowerBound = 1; lowerBound <= upperBound; lowerBound++)
{
product = product * lowerBound;
}
System.out.println("Product10: " + product);
for (lowerBound = 1; lowerBound <= (upperBound + 1); lowerBound++) {
product1 = product * lowerBound;
}
System.out.println("Product11: " + product1);
for (lowerBound = 1; lowerBound <= (upperBound + 2); lowerBound++) {
product2 = product * lowerBound;
}
System.out.println("Product12: " + product2);
for (lowerBound = 1; lowerBound <= (upperBound + 3); lowerBound++) {
product3 = product * lowerBound;
}
System.out.println("Product13: " + product3);
for (lowerBound = 1; lowerBound <= (upperBound + 4); lowerBound++) {
product4 = product * lowerBound;
}
System.out.println("Product14: " + product4);
}
}