Sum of odd numbers in c





1. C program to find sum of odd numbers
2. Sum of consecutive odd numbers


#include<stdio.h>

int main(){

    int number;
    int min,max;
    long sum =0;
  
    printf("Enter the minimum range: ");
    scanf("%d",&min);

    printf("Enter the maximum range: ");
    scanf("%d",&max);

    for(number = min;number <= max; number++)
         if(number % 2 !=0)
             sum = sum + number;

    printf("Sum of odd numbers in given range is: %ld",sum);
  
    return 0;

}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 100
Sum of odd numbers in given range is: 2500


Algorithm:

Number is called even number if it is divisible by two otherwise odd.  

Example of even numbers: 0,2,4,8,9,10 etc.
Example of odd numbers: 1, 3,5,7,9 etc.


No comments: