Friday, April 12, 2013

Sum of ODD Numbers form given Range

Sum of ODD number from Given Range 



#include <stdio.h>
void main()
{
    int index, begno, endno, sum = 0;
    printf("Program for sum of odd numbers in the given range");
    printf("Enter Beg. No.: ");
    scanf("%d", &begno);
    printf("Enter End. No.: ");
    scanf("%d", &endno);
    index = begno;
    if( (begno % 2) == 0)
        index = begno + 1;
    for(; index <= endno; index += 2)
    {
        sum = sum + index;
    }
    printf("The sum of odd 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("the sum of odd number is:%d\n",sum);
    }

    ReplyDelete
  2. #include
    main()
    {
    int i,j,n,sum=0;
    printf("Enter start num :\n");
    scanf("%d",&j);
    printf("\nEnter end num :\n");
    scanf("%d",&n);
    for(i=j;i<=n;i++)
    {
    if(i%2!=0)
    {
    sum+=i;
    }
    }
    printf("Sum of odd num from given range is :%d",sum);
    }

    ReplyDelete