Number Of Even And Odd Numbers In A Given Range

Example

#include<stdio.h>
int main()
{
       int lower, upper, i, count=0;
       printf("Enter the lower limit: ");
       scanf("%d", &lower);
       printf("\nEnter the upper limit: ");
       scanf("%d", &upper);
       for(i = lower; i <= upper; i++)
       {	
             if(i % 2 != 0)
             {	
                   count++;
             }
       }
       printf("Number of odd numbers: %d", count);
       printf("\nNumber of even numbers: %d", upper - lower + 1 - count);
}

Output:

Output

Enter the lower limit: 
Enter the upper limit: Number of odd numbers: [value]
Number of even numbers: [value]

The code functions properly with small ranges, typically up to a few hundred units. However, when attempting larger ranges with higher upper limits, the code experiences increased processing time. For instance, in the previous scenario, it required 10.45 seconds to execute.

When writing code, disregarding errors, the effectiveness of the code is consistently evaluated by:

  • Time
  • Space

We need to devise a solution with sound reasoning that is efficient.

Logic:

Let's consider scenarios and then we'll construct a formula manually:

Lower limit = L

Upper limit = R

Number of numbers in the range including both limits(N): (R - L + 1)

  • When N is even: For Example: Lower limit = 10 Upper limit = 15 N = 15 - 10 + 1 = 6 Numbers: [10, 11, 12, 13, 14, 15] Number of even numbers: N/2 = 3 = [10, 12, 16] Number of odd numbers: N/2 = 3 = [11, 13, 15]
  • When N is odd: When the lower limit and upper limit are even: For example: Lower limit = 10 Upper limit = 16 N = 16 - 10 + 1 = 7 Odd numbers = [11, 13, 15] -> 7/2 = 3 Even numbers = [10, 12, 14, 16] -> N - odd = 7 -3 = 4 When the lower limit and the upper limit are odd: For example: Lower limit = 11 Upper limit = 15 N = 15 - 11 + 1 = 5 Odd numbers = [11, 13, 15] -> 5/2 + 1 = 3 Even numbers = [12, 14] -> N - odd = 5 - 3 = 2
  • When the lower limit and upper limit are even: For example: Lower limit = 10 Upper limit = 16 N = 16 - 10 + 1 = 7 Odd numbers = [11, 13, 15] -> 7/2 = 3 Even numbers = [10, 12, 14, 16] -> N - odd = 7 -3 = 4
  • When the lower limit and the upper limit are odd: For example: Lower limit = 11 Upper limit = 15 N = 15 - 11 + 1 = 5 Odd numbers = [11, 13, 15] -> 5/2 + 1 = 3 Even numbers = [12, 14] -> N - odd = 5 - 3 = 2
  • When we have an even number of numbers in the range, half of them will be even, and the other half will be odd.
  • When we have odd number of numbers: If both lower and upper limits are even, number of odd numbers: floor(N/2) If both lower and upper limits are odd, number of odd numbers: floor(N/2) + 1
  • If both lower and upper limits are even, number of odd numbers: floor(N/2)
  • If both lower and upper limits are odd, number of odd numbers: floor(N/2) + 1

Let us now design a program with this logic in C:

Example

#include<stdio.h>
int main()
{
       int lower, upper, i, count;
       printf("Enter the lower limit: ");
       scanf("%d", &lower);
       printf("\nEnter the upper limit: ");
       scanf("%d", &upper);
       count = upper - lower + 1;
       if(count %2 == 0)
       {
               printf("Number of odd numbers: %d", count/2);
               printf("\nNumber of even numbers: %d", count/2);	
       }
       else
       {
               if(lower % 2 == 0 || upper % 2 == 0)
               {
                       printf("Number of odd numbers: %d", count/2);
                       printf("\nNumber of even numbers: %d", count - (count/2));
               }
               else
               {
                        printf("Number of odd numbers: %d", count/2 + 1);
                        printf("\nNumber of even numbers: %d", count - (count/2 + 1));
                }
        }
}

Output:

Output

Enter the lower limit: 
Enter the upper limit: Number of odd numbers: [value]
Number of even numbers: [value]
  • We checked if the number of numbers (count) is even, and this happens when the lower limit and upper limit are both of a different kind (even, odd) 3 4 5 6 2 3 4 5
  • Now, the two remaining cases are when both the upper limit and upper limit are of the same kind: 2 3 4 -> Odd numbers: 3/2 = 1 3 4 5 -> Odd numbers: 3/2 + 1 = 2

We can use Arithmetic progressions too:

By applying the formula for the n-th term of an arithmetic progression (AP): a + (n - 1) * d

We have the option to determine either the quantity of even numbers or the quantity of odd numbers, and subsequently deduce the alternative quantity by subtracting from the total number of numbers.

Let us work out the code for different cases:

Case1: Lower limit and upper limit are even:

Suppose we are determining the quantity of integers that are divisible by 2 within the range of 10 to 20:

10 11 12 13 14 15 16 17 18 19 20

Observe:

  • The numbers are in an AP with d = 1
  • The bold numbers are in another AP with d = 2
  • So, we can find the position of 20 in the bold AP to find the number of even numbers in the interval: 20 = 10 + (n - 1)2 10 = (n - 1)2 5 = (n - 1) n = 6
  • Now, 11 - 6 = 4 odd numbers.

Case 2: Lower limit and upper limit are odd

Suppose we are determining the quantity of integers that are divisible by 2 within the range of 10 to 20:

11 12 13 14 15 16 17 18 19

Observe:

  • The numbers are in an AP with d = 1
  • The bold numbers are in another AP with d = 2
  • First term = a = a + 1 = 12
  • Now, we can find the position of (last term - 1) = 18 to find the number of even numbers like in the previous case: 18 = 12 + (n - 1)2 6 = (n - 1)2 3 = (n - 1) n = 4
  • Now, number of odd numbers = 9 - 4 = 5

Case 3: Odd lower limit and even upper limit

Suppose we are determining the quantity of integers that are divisible by 2 within the range of 10 to 20:

11 12 13 14 15 16 17 18 19 20

Observe:

  • The numbers are in an AP with d = 1
  • The bold numbers are in another AP with d = 2
  • First term = a = a + 1 = 12
  • Now, we can find the position of last term = 20 to find the number of even numbers like the first case: 20 = 12 + (n - 1)2 8 = (n - 1)2 4 = (n - 1) n = 5
  • Now, number of odd numbers = 10 - 5 = 5

Case 4: Even lower limit and odd upper limit

Suppose we are determining the quantity of integers divisible by 2 within the range of 10 to 20:

12 13 14 15 16 17 18 19 20 21

Observe:

  • The numbers are in an AP with d = 1
  • The bold numbers are in another AP with d = 2
  • First term = a = 12
  • Now, we can find the position of last term -1 = 20 to find the number of even numbers like the first case: 20 = 12 + (n - 1)2 8 = (n - 1)2 4 = (n - 1) n = 5
  • Now, number of odd numbers = 10 - 5 = 5
  • When both the minimum and maximum values are even, we have the option to apply the n th -term formula immediately.
  • In cases where either the minimum or maximum value is odd, adjustments are required for the limits: the lower limit is increased by 1, and the upper limit is decreased by 1.

Program:

Example

#include<stdio.h>
int main()
{
         int lower, upper, i, even, odd;
         printf("Enter the lower limit: ");
         scanf("%d", &lower);
         printf("\nEnter the upper limit: ");
         scanf("%d", &upper);
         if(lower % 2 != 0)
         {
                 lower = lower + 1;
         }
         if(upper % 2 != 0)
         {
                 upper = upper - 1;
         }
         even = (upper - lower)/2 + 1; 
         odd = ((upper - lower) + 1) - even;
         printf("\nNumber of even numbers: %d", even);
         printf("\nNumber of odd numbers: %d", odd);
}

Output:

Output

Enter the lower limit: 10
Enter the upper limit: 20
Number of even numbers: 6
Number of odd numbers: 5

Mastering programming requires adept problem-solving abilities. Note the resemblance between the two methods we previously covered.

Input Required

This code uses input(). Please provide values below: