How can i add all the even numbers?

Asked By 20 points N/A Posted on -
qa-featured

Hi there,

How can do the sum of all even numbers from 2 to 1000000? Please write down it in programming language,no other discretions.

SHARE
Best Answer by Rodriguez Martin
Answered By 0 points N/A #161985

How can i add all the even numbers?

qa-featured

You can try by this…

for(i=o;i<=n;i+2)

{

s+=i;

}

//Or

for(i=o;i<=n;i++)

{

if(i%2==0)

{

s+=i;

}

}

Answered By 5 points N/A #161986

How can i add all the even numbers?

qa-featured

It’s better if you can specify in which programming language you want the code to be written.  Each programming language has its own method of performing the operation but with the same logic. I’ll post a code in c#.

privateint GetSum()

    {

        int sum = 0;

        for (int i = 1; i <= 1000000; i++)

        {

            sum = sum + i;

        }

         returnsum;

    }

Above method will simply return the sum of the number from 1 to 1000000. In above code the basic programming application is the use of ForLoop. Any high level language will use a loop to perform the above operation.      

Best Answer
Best Answer
Answered By 0 points N/A #161984

How can i add all the even numbers?

qa-featured

Hello Mr. Amin,

Did you mean C programming? This is a powerful language of programming system. Every programming has it's own language. The C programming code for this function is given below:

#include<stdio.h>

#include<time.h>

int main(void)

{

tine t start, end;

double sum=0,i;

start = time(Null);

for(i=2;i<=1000000;i=i+2)

sum=sum+i;

end = time(NULL);

printf("Sum: %lf/n", sum);

printf("Calculating Time: %f seconds./n", diffitime(end, start));

return 0;

}

This is the main way of doing this function. By changing the limit of 'i', you can do other works like this one.

Hope now you can do this. Thank you for your question.

Related Questions