Sum of Odd Numbers between range using scanf()


Ques: To print the sum of all odd numbers within range N1 and N2 (excluding N1 and N2). You can use scanf() for taking Input.





#include<stdio.h>
int main()
{
    int i, N1, N2, sum=0;
    printf("Enter Lower and upper range: ");
    scanf("%d%d",&N1,&N2);
    N1 = N1 + 1; // to exclude N1
    for(i = N1 ;i < N2; i++)
    {
        if(i % 2 != 0)
            sum = sum + i;

    }

    printf("\nSum is: %d",sum);
    return 0;
}



OUTPUT:  



Post a Comment

0 Comments