19.2k views
4 votes
Write two nested for loops to calculate the following double summation: ex: if s is 3 and t is 2, then summation result is 18. (1 x 1) (1 x 2) (2 x 1) (2 x 2) (3 x 1) (3 x 2) = 18?

User Key
by
8.0k points

1 Answer

4 votes

Final answer:

To calculate the double summation, you can use two nested for loops to iterate through the values of s and t. Inside the loops, multiply the current values of the loop variables and sum them to get the final result. For example, if s is 3 and t is 2, the result will be 18.

Step-by-step explanation:

To calculate the double summation described, we can use two nested for loops. The outer loop will iterate from 1 to s, and the inner loop will iterate from 1 to t. Inside the loops, we'll multiply the current values of the loop variables to calculate the product and sum it to a running total.

Here's an example of the code:

int s = 3;
int t = 2;
int result = 0;

for (int i = 1; i <= s; i++) {
for (int j = 1; j <= t; j++) {
int product = i * j;
result += product;
}
}

In this case, the result will be 18.

User Antony Woods
by
7.8k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.

9.4m questions

12.2m answers

Categories