How to add all even and odd separately?

CODE




How to add all even and odd separately?
Program to add all even and odd values separately from 1 to N.

int main()
{
    int n,e=0,o=0,i;
    printf("\n enter the number:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        if(i%2==0)
            e+=i;
        else
            o+=i;
    }
    printf("\n sum of evens=%d",e);
    printf("\n sum of odd=%d",o);
}


Output:
enter the number:5

 sum of evens=6
 sum of odd=9

How to add all even and odd separately? How to add all even and odd separately? Reviewed by Unknown on December 16, 2018 Rating: 5

No comments:

If you have any doubt or query ,comment below:

Programming copyright © 2018-19. Powered by Blogger.