In: Computer Science
Please write code in C, thank you.
Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers.
Ex: If the input is:
5 2 4 6 8 10
the output is:
all even
Ex: If the input is:
5 1 3 5 7 9
the output is:
all odd
Ex: If the input is:
5 1 2 3 4 5
the output is:
not even or odd
Your program must define and call the following two functions.
IsArrayEven returns true if all integers in the array are even and
false otherwise. IsArrayOdd returns true if all integers in the
array are odd and false otherwise.
bool IsArrayEven(int inputVals[], int numVals)
bool IsArrayOdd(int inputVals[], int numVals)
Code:
#include<stdio.h>
#include<stdbool.h>
//for
boolean
bool IsArrayOdd(int array[],int n)
{
for(int i=0;i<n;i++)
{
if(array[i]%2==0)
//if any even number
encounter then return false
return
false;
}
return true;
//if no
even number then return true
}
bool IsArrayEven(int array[],int n)
{
for(int i=0;i<n;i++)
{
if(array[i]%2!=0)
//if any odd number encounter
then return false
return
false;
}
return true;
//if no
odd number then return true
}
int main()
{
int n;
scanf("%d",&n);
//size of
array
int array[n];
int i,j;
for(i=0;i<n;i++)
{
scanf("%d",&array[i]); //array
input
}
i=IsArrayOdd(array,n);
//calling function
j=IsArrayEven(array,n);
//calling function
if(j)
//if j is true
printf("all even\n");
else if(i)
//if i is true
printf("all odd\n");
else
//if i and j are false
printf("not even or odd\n");
}