Exercise 2: IsEven function (10 points) Write a function named isEven that takes
ID: 3596494 • Letter: E
Question
Exercise 2: IsEven function (10 points) Write a function named isEven that takes an integer argument n and returns a boolean value. Implement the function so that it returns true when n is even and false when n is odd. Write a program that calls the isEven function to test it. Exercise 4: Random integer function (10 points) Implement a function named randomInteger that takes as arguments a lower bound a and an upper bound b of an interval [a, b]. The function computes and returns a random integer that falls inside this interval. Write a program that prompts the user for a lower bound and an upper bound. After the user inputs these values, the program should call your randominteger function to get a random integer between the lower and upper bounds. The program displays this value to the user and then terminates.Explanation / Answer
#include <iostream>
using namespace std;
bool isEven(int n) {
return n % 2 == 0;
}
int main()
{
int n;
cout << "Enter an integer: " << endl;
cin >> n;
if(isEven(n)) {
cout<<n<<" is even."<<endl;
} else {
cout<<n<<" is odd."<<endl;
}
return 0;
}
Output:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int randominteger(int l, int u) {
srand (time(NULL));
return rand() % (u-l) + l;
}
int main()
{
int l,u;
cout<<"Enter the lower limit: "<<endl;
cin >> l;
cout<<"Enter the uppwer limit: "<<endl;
cin >> u;
cout<<"Random number: "<<randominteger(l,u)<<endl;
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.