C program to find sum of first n even positive numbers

C program to find sum of first n even positive numbers ?

Sol.

/**
 * C program to print the sum of first n even positive numbers
 */

#include <stdio.h>

int main()
{
    /* Even numbers are integers */
    int count, n, sumOfEvenNum=0;
    
    /* First positive even numbers starts from 0 and end at n. */
    printf("Enter positive value of n : \n");
    scanf("%d", &n);
    
    /* Find the sum of first n even positive numbers */
    /*Positive even numbers are 0, 2, 4, 6....
    * So increment count by 2 each time */
    
    for(count=0; count<=n; count=count+2)
    {
        sumOfEvenNum = sumOfEvenNum + count;
    }

    printf("Sum of first n even numbers = %d", sumOfEvenNum);

    return 0;
}

Output :



Share:

Post a Comment

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.