Question

Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven-...

Write C++ Code in single source file (.cpp file) containing following user defined functions:

1. IsEven- takes an integer input (one argument of type int) and return true or false.

2. IsPositive- takes a float input (one argument of type double) and return a Boolean value. The function returns the true if its argument is positive and false if its argument is zero or negative.

3. IsPrime- takes an integer input and return true or false.

4. NumOfDigits- takes an integer input and return number of digits in that integer.

Instructions: After writing these functions, show usage of these functions in main () function by calling these functions and showing their output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<iomanip>

using namespace std;

bool isEven(int num){
if(num%2 == 0){ //if even, return true
return true;
}
return false; //else return false
}

bool isPositive(double num) {
if(num>0){ //if positive number, return true
return true;
}
return false; //else return false
}

bool isPrime(int num){
for(int i=2;i<=num/2;i++){
if(num%i == 0){
return false; //not prime no
}
}
return true; //return true, prime
}

int NumOfDigits(int num){
int n = 0;
while(num!=0){
n++;//count digit
num = num/10;
}
return n;
}

int main() //main function
{
cout<<"isEven(4): "<<std::boolalpha <<isEven(4)<<endl;
cout<<"isEven(3): "<<isEven(3)<<endl;
cout<<"isPositive(4.0): "<<isPositive(4.0)<<endl;
cout<<"isPositive(-4.0): "<<isPositive(-4.0)<<endl;
cout<<"isPrime(4): "<<isPrime(4)<<endl;
cout<<"isPrime(3): "<<isPrime(3)<<endl;
cout<<"NumOfDigits(434): "<<NumOfDigits(434)<<endl;
return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven-...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python file containing the following functions. Also turn in the output from testing the...

    Write a Python file containing the following functions. Also turn in the output from testing the functions. All arguments to the functions may be hard-coded. Function 1 takes a list of strings as a parameter and returns a list of strings consisting of all the strings in the original list that have identical consecutive letters. For example: fun1 ([llama, good, cat, abba, abab, 112, dog]) returns [llama, good, abba, 112] Function 2 takes an integer (n), representing the number of...

  • Part (A) Note: This class must be created in a separate cpp file Create a class...

    Part (A) Note: This class must be created in a separate cpp file Create a class Employee with the following attributes and operations: Attributes (Data members): i. An array to store the employee name. The length of this array is 256 bytes. ii. An array to store the company name. The length of this array is 256 bytes. iii. An array to store the department name. The length of this array is 256 bytes. iv. An unsigned integer to store...

  • Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a...

    Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a print(text, number) function and the main() function. The power(x,y) function returns an integer result that is calculated by raising a number x (integer) to a power y (integer). The second argument y (exponent) of the function can not exceed 100. If the second argument exceeds 100, the function should return -1. Your power(x,y) function must be able to take either 1 or 2 integer...

  • Note: The order that these functions are listed, do not reflect the order that they should...

    Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...

  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • C Programming 12 1 point We can write a large amount text data to a file...

    C Programming 12 1 point We can write a large amount text data to a file without punctuation and will be able to make sense of that data when looking at the file with notepad. True False 13 1 point a When calling the function to open a file in read mode can cause the contents of the file to be erased. True False 14 1 point Writing a struct to a binary file is similar to writing an integer...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Write a C program as follows: Single source code file Requests the user to input two...

    Write a C program as follows: Single source code file Requests the user to input two integer numbers Requests the user to make a choice between 0 (add), 1 (subtract), or 2 (multiply) Declares three separate functions Uses a pointer to these three functions to perform the requested action Outputs the result to the screen Submit your program source code file to this assignment. Sample Output Enter first integer number: 15 Enter second integer number: 10 Enter Choice: 0 for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT