Respuesta :
Answer:
The c++ program to show the implementation of parameterized method returning Boolean value is shown below.
#include<iostream>
using namespace std;
bool areFactors(int n, int arr[]);
int main()
{
int num=34;
int factors[5]={2, 3, 5};
bool result;
result = areFactors(num, factors);
if(result == true)
cout<<"The number "<<num<<" is divisible by all factors."<<endl;
else
cout<<"The number "<<num<<" is not divisible by all factors."<<endl;
return 0;
}
bool areFactors(int n, int arr[])
{
bool divisible=true;
int flag=0;
for(int i=0; i<3; i++)
{
if(n%arr[i] != 0)
{
divisible = false;
flag ++;
}
}
if(flag == 0)
return true;
else
return false;
}
OUTPUT
The number 34 is not divisible by all factors.
Explanation:
This program shows the implementation of function which takes two integer parameters which includes one variable and one array. The method returns a Boolean value, either true or false.
The method declaration is as follows.
bool areFactors(int n, int arr[]);
This method is defined as follows.
bool areFactors(int n, int arr[])
{
bool divisible=true;
// we assume number is divisible by all factors hence flag is initialized to 0
int flag=0;
for(int i=0; i<3; i++)
{
if(n%arr[i] != 0)
{
divisible = false;
// flag variable is incremented if number is not divisible by any factor
flag ++;
}
}
// number is divisible by all factors
if(flag == 0)
return true;
// number is not divisible by all factors
else
return false;
}
The Boolean value is returned to main method.
bool result;
result = areFactors(num, factors);
This program checks if the number is divisible by all the factors in an array.
There is no input taken from the user. The number and all the factors are hardcoded inside the program by the programmer.
The program can be tested against different numbers and factors. The size of the array can also be changed for testing.