Create a program that: 1) Displays a menu of functions that the user is able to
ID: 3871616 • Letter: C
Question
Create a program that:
1) Displays a menu of functions that the user is able to pick from. If information is returned from a function (ie NOT a void function), then that information should be displayed outside of the function. Any information passed as an argument (in the parenthesis) should be obtained outside of the function and passed in when called.
a. void hello()
i. Will simply display the text “Hello, world!”
b. void printBetween(int a, int b)
i. Will print all numbers between a and b, including a and b, counting up
-- E.G. a = 3, b = 6, ouput = “3 4 5 6” (or can use newlines)
-- Order is unassumed, but data is displayed counting up
c. bool isPrime(int n)
i. Returns true to indicate n is prime, or false to indicate it’s not
-- A number n is prime if it is greater than 1 and not divisible by any of the numbers x in the range 1 < x < n
d. int leastCommonDenominator(int a, int b)
i. Returns the smallest number that is divisible by both a and b
-- Which means (answer % a) and (answer % b) are both 0
-- The least common denominator of 3 and 4 is 12
e. void squaredOpposite(double &n)
i. Sets the contents of the argument to be the squared value of n, with the opposite sign compared to n
-- E.G. input of 3 would result in output of -9
-- n is being passed as a reference variable, meaning changes made to it in the function will be reflected in the argument that was passed into the function call
2) Asks the user if they would like to return to step 1, or exit.
Explanation / Answer
Program:-
#include<iostream>
#include<cstdlib>
using namespace std;
void hello()
{
cout<<"Hello, world! ";
}
void printBetween(int a, int b)
{
int temp,i;
if(a>b)
{
temp=a;
a=b;
b=temp;
}
for(i=a;i<=b;i++)
cout<<i<<" ";
cout<<" ";
}
bool isPrime(int n)
{
int i;
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
return false;
}
}
return true;
}
int leastCommonDenominator(int a, int b)
{
int answer=1;
while(answer>=1)
{
if((answer % a)==0 && (answer % b)==0)
return answer;
answer++;
}
}
void squaredOpposite(double &n)
{
int ans=(n)*(n);
if(n>=0)
ans=ans*-1;
n=ans;
}
int main()
{
int ch,a,b,lcm;
double n;
bool f;
do{
cout<<" 1.hello world 2.printBetween 3.isPrime 4.leastCommonDenominator 5.squaredOpposite 6.exit Enter your choice :";
cin>>ch;
switch(ch)
{
case 1:
hello();
break;
case 2:
cout<<"Enter two Numbers: ";
cin>>a>>b;
printBetween(a,b);
break;
case 3:
cout<<"Enter a Number: ";
cin>>a;
f=isPrime(a);
if(f==true)
cout<<"Entered Number is Prime ";
else
cout<<"Entered Number is Not Prime ";
break;
case 4:
cout<<"Enter two Numbers: ";
cin>>a>>b;
lcm=leastCommonDenominator(a,b);
cout<<"Answer is: "<<lcm<<endl;
break;
case 5:
cout<<"Enter a Number :";
cin>>n;
squaredOpposite(n);
cout<<n;
break;
case 6:
exit(0);
default:
cout<<"wrong Choice try again";
}
}while(ch>0&&ch<6);
return 0;
}
Output:--
1.hello world
2.printBetween
3.isPrime
4.leastCommonDenominator
5.squaredOpposite
6.exit
Enter your choice :1
Hello, world!
1.hello world
2.printBetween
3.isPrime
4.leastCommonDenominator
5.squaredOpposite
6.exit
Enter your choice :2
Enter two Numbers: 2 8
2 3 4 5 6 7 8
1.hello world
2.printBetween
3.isPrime
4.leastCommonDenominator
5.squaredOpposite
6.exit
Enter your choice :2
Enter two Numbers: 9 5
5 6 7 8 9
1.hello world
2.printBetween
3.isPrime
4.leastCommonDenominator
5.squaredOpposite
6.exit
Enter your choice :3
Enter a Number: 7
Entered Number is Prime
1.hello world
2.printBetween
3.isPrime
4.leastCommonDenominator
5.squaredOpposite
6.exit
Enter your choice :3
Enter a Number: 6
Entered Number is Not Prime
1.hello world
2.printBetween
3.isPrime
4.leastCommonDenominator
5.squaredOpposite
6.exit
Enter your choice :4
Enter two Numbers: 3 4
Answer is: 12
1.hello world
2.printBetween
3.isPrime
4.leastCommonDenominator
5.squaredOpposite
6.exit
Enter your choice :5
Enter a Number :Enter a Number:3
-9
1.hello world
2.printBetween
3.isPrime
4.leastCommonDenominator
5.squaredOpposite
6.exit
Enter your choice :6
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.