Javascript to find sum of first n even number
Answers
Answered by
2
"import java.util.*;
import java.lang.*;
public class GfG{
// function to find sum of
// first n even numbers
static int evenSum(int n)
{
return (n * (n + 1));
}
public static void main(String argc[])
{
int n = 20;
System.out.println(""Sum of first "" + n +
"" Even numbers is: "" +
evenSum(n));
}
}
"
Answered by
2
Hi
const getSomeOfFirstNEvenNumbers = range => {
let total = 0;
for (var i = 0; i <= range; i++) {
if (i % 2 == 0){
total += i;
} else {
continue;
}
}
console.log(total);
}
getSomeOfFirstNEvenNumbers(5);
Attachments:
Similar questions
Environmental Sciences,
7 months ago
Math,
7 months ago
English,
1 year ago
English,
1 year ago
Environmental Sciences,
1 year ago
Physics,
1 year ago