Ask Question
7 December, 03:08

Write a for loop that computes the following sum: 5+10+15+20 + ... + 485+490+495+500. The sum should be placed in a variable sum that has already been declared and initialized to 0. In addition, there is another variable, num that has also been declared. You must not use any other variables.

+4
Answers (1)
  1. 7 December, 03:31
    0
    public class num9 {

    public static void main (String[] args) {

    int sum = 0;

    int num = 5;

    for (num = 5; num<=500; num+=5) {

    System. out. println (num);

    sum + =num;

    }

    System. out. println (sum);

    }

    }

    Explanation:

    Declare and initialize the variables sum and num Use a for loop with the condition for (num = 5; num<=500; num+=5) Since the loop will increment by 5 and run from 5 to 500 Within the loop, add num to sum at each iteration Print all elements on seperate lines Outside the loop, print the final value of sum

    Create a for loop
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a for loop that computes the following sum: 5+10+15+20 + ... + 485+490+495+500. The sum should be placed in a variable sum that has ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers