Question

In: Computer Science

Write a program that has a function (named getNumStats()) that will return a string that has...

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)

Solutions

Expert Solution

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;
}


Related Solutions

3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Write a PHP program that checks the elements of a string array named $Passwords. Use regular...
Write a PHP program that checks the elements of a string array named $Passwords. Use regular expressions to test whether each element is a strong password. For this exercise, a strong password must have at least one number, one lowercase letter, one uppercase letter, no spaces, and at least one character that is not a letter or number. (Hint: Use the [^0-9A-Za-z] character class.) The string should also be between 8 and 16 characters long. The $Passwords array should contain...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt the user to enter the sales amount and computes and prints with a description the commission paid to salesperson as follows: 10% for sales amount less than $2,000.00, 15% for sales amount less than $10,000.00 and 20% for sales amount less than $20,000.00, then function calc_commission calls another function name assign_base_salary() to ask the user to enter each of 5 salesperson’s base salary ,...
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
Write a program that receives an integer and must return a string containing the hexadecimal representation...
Write a program that receives an integer and must return a string containing the hexadecimal representation of that integer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT