Questions about functions And some sample outputs Task 1 Write a program named p
ID: 3757518 • Letter: Q
Question
Questions about functions
And some sample outputs
Task 1 Write a program named prime.cpp that: Implements a function named isPrime () that returns a Boolean value to indicate whether or not an input number is prime (divides evenly into only itself and 1) * o Return type: bool o Parameter(s): int Calls the isPrime () function to generate a list of all prime numbers from 2 to 100 (inclusive) Task 2 Write a program named calculator.cpp that: Prompts the user for two floating point number operands and one math operator (+, -, *, /,) o Validate the user input for the math operators o Validate the user input for divide by 0 errors .Implements a function named calc() that determines the result of the mathematical operation o Return type: double o Parameter(s): double (operand1), char (operator), double (operand2) Calls the calc () function to display the result of the operation in a well-formatted output Prompts the user to decide whether to calculate another operation or not . ° The calc) function must NOT display any prompts, accept any user input, or display the result of the operationIs puposei smplyt calculate the mathematical operationExplanation / Answer
If you post more than 1 question, as per chegg guidelines I have to solve only first question.
Ques 1.
#include<iostream>
using namespace std;
bool isPrime(int n)
{
// any no smaller than 2 is not prime
if( n < 2 )
return false;
// 2 is only even prime
else if( n == 2 )
return true;
// even no other than 2 are not prime
else if( n % 2 == 0 )
return false;
int i;
for( i = 3 ; i < n ; i += 2 )
// if n is divisible by i
if( n % i == 0 )
return false;
return true;
}
int main()
{
int i;
cout<<"Prime Numbers : ";
for( i = 2 ; i <= 100 ; i++ )
if( isPrime(i) )
cout<<i<<" ";
return 0;
}
Sample Output
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.