Program to find even and odd number between range m to n and find their sum using while loop

Write a program to find even and odd numbers between range m to n and find their sum using the while loop.

C Program

#include <stdio.h>
int main()
{
    int mn;
    printf("Enter starting number : ");
    scanf("%d", &m);
    printf("Enter ending number : ");
    scanf("%d", &n);
    int i = m;
    int even = 0odd = 0;
    int evenNum[n - m], oddNum[n - m];
    int l = 0r = 0;
    while (i <= n)
    {
        if (i % 2 == 0)
        {
            even += i;
            evenNum[l] = i;
            l++;
            i++;
        }
        else
        {
            odd += i;
            oddNum[r] = i;
            r++;
            i++;
        }
    }
    printf("Even numbers are :\n");
    for (i = 0i < li++)
    {
        printf("%d "evenNum[i]);
    }
    printf("\n");
    printf("Odd numbers are :\n");
    for (i = 0i < ri++)
    {
        printf("%d "oddNum[i]);
    }
    printf("\n");
    printf("Sum of even numbers from %d to %d is %d\n"mneven);
    printf("Sum of odd numbers from %d to %d is %d\n"mnodd);
    return 0;
}
Input:

Enter starting number : 5
Enter ending number : 12
Output:

Even numbers are :
6 8 10 12 
Odd numbers are :
5 7 9 11 
Sum of even numbers from 5 to 12 is 36
Sum of odd numbers from 5 to 12 is 32


No comments:

Post a Comment