asked 45.2k views
1 vote
Create a program in JS that utilizes a for loop to sum the odd number from 500 to 1000, one number per iteration

2 Answers

4 votes

Answer:

let sum = 0;

for (let i = 501; i < <= 1000; i+=2)

{

sum += i;

}

console.log("The sum off odd numbers, from 500 to 1000, is " + sum);

answered
User Benjimin
by
7.8k points
6 votes

Final answer:

To sum the odd numbers from 500 to 1000 using a for loop in JavaScript, you can set up a variable to keep track of the total sum, and then use the for loop to iterate through each number in the given range.

Step-by-step explanation:

A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop.

A for statement looks as follows: js Copy to Clipboard for (initialization; condition; afterthought) statement.

To sum the odd numbers from 500 to 1000 using a for loop in JavaScript, you can set up a variable to keep track of the total sum, and then use the for loop to iterate through each number in the given range.

Inside the loop, you can use an if statement to check if the current number is odd. If it is, you can add it to the total sum.

After the loop finishes, you can print out the final sum.

let sum = 0;
for (let i = 500; i <= 1000; i++) {
if (i % 2 !== 0) {
sum += i;
}
}
console.log(sum);
answered
User Muzamil Abbas
by
8.3k points
Welcome to Qamnty — a place to ask, share, and grow together. Join our community and get real answers from real people.