In: Computer Science
Prompt the user to enter an integer
Then, prompt the user to enter a positive integer n2.
Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd
If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below.
Sample:
Enter n1: -2
Enter n2: 7
Enter 7 values: -2 3 3 -2 4 3 6
4 is even
3 is odd
6 is even
Enter n1: 100
Enter n2: 6
Enter 6 values: 101 2 -7 100 9100
No values after 100
Enter n1: 5
Enter n2: 3
Enter 3 values: 4 5 104
9 does not occur
Program:
#include <iostream>
using namespace std;
int main()
{
int n,n2;
int count = 0;
cout<<"Enter n1: ";
cin>>n;
cout<<"Enter n2: ";
cin>>n2;
int numArr[n2];
int lastIndex;
cout<<"Enter "<<n2<<" values: ";
for(int i=0;i<n2;i++)
{
cin>>numArr[i];
}
for(int i=0;i<n2;i++)
{
if(numArr[i]==n)
{
lastIndex = i;
}
else
{
count++;
}
}
if(lastIndex == n2-1 && count!=n2)
{
cout<<"No values after "<<n;
}
else if(count==n2)
{
cout<<n<<" does not occur";
}
else
{
for(int i=lastIndex+1;i<n2;i++)
{
if(numArr[i]%2==0)
{
cout<<numArr[i]<<" is even"<<endl;
}
else
{
cout<<numArr[i]<<" is odd"<<endl;
}
}
}
return 0;
}
Output: