Skip to content

Instantly share code, notes, and snippets.

@mjcarnaje
Last active December 7, 2021 13:30
Show Gist options
  • Save mjcarnaje/74895e609a2ee22c2eba39d6fac934b4 to your computer and use it in GitHub Desktop.
Save mjcarnaje/74895e609a2ee22c2eba39d6fac934b4 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void odd_numbers(int first, int second)
{
for (int i = first; i <= second; i++)
{
if (i % 2 != 0)
{
printf("%d ", i);
}
}
}
void even_numbers(int first, int second)
{
for (int i = first; i <= second; i++)
{
if (i % 2 == 0)
{
printf("%d ", i);
}
}
}
int main()
{
int first, second, type;
int invalid_count = 0;
printf("Enter a number: ");
scanf("%d", &first);
printf("Enter another number: ");
scanf("%d", &second);
if (first >= second)
{
printf("\nInvalid input range.\n");
return 0;
}
do
{
if (invalid_count > 0)
{
printf("\nInvalid input.\n");
}
printf("Enter [1] to use display even [2] to display odd: ");
scanf("%d", &type);
invalid_count++;
} while (type != 1 && type != 2);
if (type == 1)
{
printf("\nEven numbers between %d and %d:\n", first, second);
even_numbers(first, second);
}
else if (type == 2)
{
printf("\nOdd numbers between %d and %d:\n", first, second);
odd_numbers(first, second);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment