Question

In: Computer Science

Prompt the user to enter an integer Then, prompt the user to enter a positive integer...

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

Solutions

Expert Solution

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:


Related Solutions

A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Prompt user to enter an integer number from console. Use 3 methods to check if this...
Prompt user to enter an integer number from console. Use 3 methods to check if this number is even or odd. Display result. This is required to be done in MIPS assembly language.
Make a program that lets the user enter a positive or negative integer as many times...
Make a program that lets the user enter a positive or negative integer as many times as they want. java //import statement //class header //Begin class scope //Method header for main. //Begin method scope. //Declare input object for Scanner. //Declare variable called entry initialized to zero. //Declare variable called response initialized to blank space. //Prompt "Do you want to enter an integer? Y or N: " //Store value in correct variable.   //while header that tests uppercase in response //Begin scope...
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number by factor 7, display result. This is required to be done in MIPS Assembly Language.
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once The program should output the average, largest, and smallest of 5 numbers. You must implement the methods listed below in your program. static float getAverage(int[] data) {...} static int getLargest(int[] data) {...} static int getSmallest(int[] data) {...}
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT