Write a program to display sum of even numbers from 1-n

//this is a program to display sum of even numbers from 1-n
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0,n;

printf("\n\nEnter any number: ");
scanf("%d",&n);

printf("\n\nPrinting Sum of all the Even numbers from 1-%d",n);

    while(i<=n)
{
if(i%2==0)
{
sum=sum+i;
}
i++;
}

printf("\n\nSum of Even Numbers from 1-%d=%d",n,sum);

getch();
}


Previous
Next Post »