In: Computer Science
Write a program that has a function (named getNumStats()) that will return a string that has stats on the number entered.
The prototype is:
string getNumStats(double);
The stats are:
Is the number an integer or a double?
The polarity (is it positive or negative; 0 counts as positive)
The parity (is it odd or even)
Is it a prime number? (for this program, 1 is considered a prime number)
For example, if the following lines of code are executed
double dNum = 14.06;
string test = getNumStats(dNum)
cout << test << endl;
The output that appears on the screen will be:
14.06 is a double
It is positive
It does not have parity
It is not a prime number
Another run is:
double dNum = -27.371;
string test = getNumStats(dNum)
cout << test << endl;
The output that appears on the screen will be:
-23.371 is a double
It is negative
It does not have parity
It is not a prime number
Note: your first line of output may or may not show trailing zeros. You may add that feature to always show zeros (even if the number is an integer)
string getNumStats(double num)
{
string s="";
int intCheck=(int(num)==num);
if (intCheck==1)
{
s=s+to_string(num)+" is an
integer";
}
else
{
s=s+to_string(num)+" is a
double";
}
int polarityCheck=(num>=0);
if(polarityCheck==1)
{
s=s+"\nIt is positive";
}
else
{
s=s+"\nIt is negative";
}
int isEven=0;
if (intCheck==1 && (int(num)%2==0))
isEven=1;
else if(intCheck==1 && (int(num)%2==1))
isEven=0;
else
isEven=0;
if(isEven==0)
{
s=s+"\nIt does not have
parity";
}
else
{
s=s+"\nIt has parity";
}
int primeCheck=0;
if(intCheck==1 && polarityCheck==1)
{
primeCheck=1;
if(int(num)==1)
primeCheck=1;
else
{
for(int
i=2;i<int(num);i++)
{
if(int(num)%i==0)
{
primeCheck=0;
break;
}
}
}
}
if(primeCheck==1)
s=s+"\nIt is a prime number";
else
s=s+"\nIt is not a prime number";
return s;
}