Sunday, 12 November 2017

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:  



No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...