WAP in C to find sum of all even numbers between 1 to n using for loop?

#include<stdio.h>
#include<conio.h>
int main()
{
int i, n, sum = 0;
clrscr();
// Take a positive number as input form user
printf(“Enter a Positive Number\n”);
scanf(“%d”, &n);
for(i= 1; i <= n; i++)
{
/* Even numbers are divisible by 2 */
if(i%2 == 0)
{
sum = sum + i;
}
}
printf(“Sum of all Even numbers between 1 to %d is %d”, n, sum);
getch();
return(0);
}

Output –

foresum

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s