Computer Science, asked by TbiaSamishta, 1 year ago

Javascript to find sum of first n even number

Answers

Answered by aqibkincsem
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 Anonymous
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