Write a program acts as a utility to allow users to enter a test number and dete
ID: 3669484 • Letter: W
Question
Write a program acts as a utility to allow users to enter a test number and determine its sign, even/odd state, and status as a prime number. The program should begin with the number unset (indicated with a boolean flag). An option should be present that allows the user to enter a number to test (option #1). Once a number is set, the menu is redisplayed, except now options to test the number for the previously mentioned properties are present. The user makes a selection and an appropriate message is displayed on the screen. Place the main logic for your program in main. Be sure to include separate functions for displaying the menu, setting the test number and performing the tests. A starting shell and sample output are provided below. Make your program models the sample as closely as possible. Pay special attention to the way the menus display in an intelligent fashion.
// INCLUDES AND NAMESPACES
#include <iostream>
using namespace std;
// DEFINES
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif
// FUNCTION PROTOTYPES
int DisplayMenuSelections();
void SetTestNumber();
void SignTest();
void EvenOddTest();
void PrimalityTest();
// GLOBALS
int gTestNumber = 0; // global variable that holds the number we are testing
bool gTestNumberSet = FALSE; // global variable that contains a boolean state reflecting
// whether or not the user has set the test number
// MAIN
int main() {
return 0;
}
// FUNCTION IMPLEMENTATIONS
// returns the user's menu selection code (-1 if invalid selection is made)
int DisplayMenuSelections() {
}
void SetTestNumber() {
}
void SignTest() {
}
void EvenOddTest() {
}
void PrimalityTest() {
}
Sample screenshots:
Explanation / Answer
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
/*DEFINES*/
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif
/* FUNCTION PROTOTYPES */
int DisplayMenuSelections();
void SetTestNumber();
void SignTest();
void EvenOddTest();
void PrimalityTest();
/* GLOBALS */
int gTestNumber = 0; /* global variable that holds the number we are testing */
bool gTestNumberSet = FALSE; /* global variable that contains a boolean state reflecting */
/* whether or not the user has set the test number*/
/* MAIN */
int main() {
int n ;
while(1){
n = DisplayMenuSelections() ;
if(n != -1){
if(n==5)
break;
switch(n) {
case 1 : SetTestNumber() ;
break;
case 2 : SignTest() ;
break;
case 3: EvenOddTest();
break;
case 4: PrimalityTest();
break;
}
}
}
return 0;
}
/* FUNCTION IMPLEMENTATIONS */
/* returns the user's menu selection code (-1 if invalid selection is made) */
int DisplayMenuSelections() {
int op;
if(gTestNumberSet == FALSE){
printf("MENU ---- ");
printf("1.Set Test Number ");
scanf("%d",&op);
if(op == 1)
return 1;
else
return -1;
}
else {
printf(" MENU ---- ");
printf("1.Set Test Number 2.Sign Test 3.EvenOddTest 4.PrimalityTest 5.Exit ");
scanf("%d",&op);
if(op == 1 || op == 2 || op == 3 || op == 4 || op == 5)
return op;
else
return -1;
}
}
void SetTestNumber() {
printf(" Enter a number ");
scanf("%d",&gTestNumber);
gTestNumberSet = TRUE;
}
void SignTest() {
if(gTestNumber<0)
printf(" The number %d is Negative",gTestNumber);
else if(gTestNumber>0)
printf(" The number %d is positive",gTestNumber);
else
printf(" 0 : Neither positive nor negative ");
}
void EvenOddTest() {
if(gTestNumber %2 == 0)
printf(" The number %d is Even",gTestNumber);
else
printf(" The number %d is Odd",gTestNumber);
}
void PrimalityTest() {
for(int i=2;i<sqrt(gTestNumber);i++)
if(gTestNumber % i == 0){
printf(" The number %d is Not prime",gTestNumber);
return;
}
if(gTestNumber>2)
printf(" The number is %d Prime",gTestNumber);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.