25. WRITE A C PROGRAM FIND SUM OF EVEN NUMBERS

/* C program to print sum of allEVEN numbers between 1 to n*/

#include<stdio.h>
#include<conio.h>
int main()
{
int i, n, sum=0;

/* Reads range from user */
printf("Enter any number: ");
scanf("%d", &n);

for(i=2; i<=n; i+=2)
{
/* Adds each subsequentEVEN numbers to sum */
sum += i;
}

printf("\nSum of all even number between 1 to %d = %d", n, sum);

return 0;
}

output

Enter any number: 10
Sum of all even number between 1 to 10 = 30

Comments