Sum of even numbers from 1 to N c program



#include<stdio.h>
int main()
{
    int sum = 0, n;
    printf("Enter the number : ");
    scanf("%d", &n);
    // Using Math formula
    // (n/2)((n / 2) + 1)
    sum = ((n / 2) * ((n / 2) + 1));
    printf("Sum of even numbers between 1 to %d : %d", n, sum);
    return 0;
}


Output : 

Enter the number : 100
Sum of even numbers between 1 to 100 : 2550

0 Comments