Sum of odd and even numbers c program






#include<stdio.h>

int main(){

    int number;
    int min,max;
    long odd_sum =0,even_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)
             odd_sum = odd_sum + number;
         else
             even_sum = even_sum + number;

    printf("Sum of even numbers in given range is: %ld\n",even_sum);
    printf("Sum of odd numbers in given range is: %ld",odd_sum);
  
    return 0;

}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Sum of even numbers in given range is: 30
Sum of odd numbers in given range is: 25



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.




Post a Comment