Untitled
user_3554110889
java
3 years ago
1.9 kB
14
Indexable
/*
1. Create a file name in the format Activity4_(your lastname).java.
2. Create a java code that will generate the following requirements.
Requirements:
a. Input/Initialize a variable that will hold a value for the initial number.
Example, int startNumber = 1;
b. Input/Initialize another variable for the number of iterations of the initial number in a series.
Example, int iteration = 10;
c. Input/Initialize another variable for the common difference for each iteration.
Example, int commonDifference = 5;
d. Concatenate the result using '+' and '*' and '=' operands.
Example, 1 + 6 + 11 + 16 + 21 + 26 + 31 + 36 + ...
e. Compute for the sum and product of the series.
Example, 1 + 6 + 11 + 16 + 21 + 26 + 31 + 36 + 41 + 46 = 235
3. The displayed output/s should look like this:
Initial Number: 1
No. of Iterations: 10
Common Difference: 5
Sum: 1 + 6 + 11 + 16 + 21 + 26 + 31 + 36 + 41 + 46 = 235
Product: 1 * 6 * 11 * 16 * 21 * 26 * 31 * 36 * 41 * 46 = 1,213,563,326,976
4. Submit the java file/s and screenshot/s of your activity.
*/
public class Activity4_Pine {
public static void main(String[] args) {
// Inputs
int num = 1;
int count = 10;
int diff = 5;
// additional variables
String sum = "", prod = "";
int totalsum = 0, totalprod = 1;
// loop
for (int i = num; i <= count * diff; i += diff) {
sum = sum + (sum != "" ? " + " + i : i);
totalsum += i;
prod = prod + (prod != "" ? " * " + i : i);
totalprod *= i;
}
// output
System.out.println("Sum: " + sum + " = " + totalsum);
System.out.println("Product: " + prod + " = " + totalprod);
}
}Editor is loading...