Friday, April 12, 2013

Sum of EVEN number from given range

Sum of EVEN Numbers 



#include <stdio.h>
void main()
{
    int index, begno, endno, sum = 0;
    printf("Program for sum of even numbers in the given range\n");
    printf("Enter Beg. No.: ");
    scanf("%d", &begno);
    printf("Enter End. No.: ");
    scanf("%d", &endno);
    index = begno;
    if( (begno % 2) == 1) // If it ODD, then make it EVEN
        index = begno + 1;
    for(; index <= endno; index += 2)
    {
        sum = sum + index;
    }
    printf("The sum of even numbers between %d and %d is: %d", begno, endno, sum);
}

2 comments:

  1. #include
    main()
    {
    int i,n,sum=0;
    printf("Enter the range:\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    if(i%2==0)
    sum+=i;
    }
    printf("sum of even number is:%d",sum);
    return 0;
    }

    ReplyDelete
  2. #include
    #include
    main()
    {
    int n,a,b,sum=0;
    printf("Enter the number of start:\n");
    scanf("%d",&a);
    printf("Enter the number of end:\n");
    scanf("%d",&b);
    for(n=a;n<=b;n++);
    {
    if(n%2==0)
    {
    sum=sum+n;
    }
    }
    printf("sum of even number from given range is:%d",sum);
    }

    ReplyDelete