Question

In: Computer Science

create a c++ proram to accept a positive number in the main program. Create and call...

create a c++ proram to accept a positive number in the main program. Create and call the following functions 1.

1. OddorEven -to determine if the number is an odd or even number ( do not use $ or modul opeartor) and will return " even number " or " odd number ".

2. Factorial - to compute the factorial of N.

3. Power - to compute the power of N without using pow function.

display the result in the main function.

use only #include <iostream>

Solutions

Expert Solution

C++ PROGRAM

#include<iostream>
using namespace std;

// implement evenRodd() function with single integer parameter
// return type string
// and this is implement without using modulus or $
string evenRodd(int n)
{
   // check condition with bitwise & with 1
   // e.g., 2 binary value 0010 & 0001 = 0
   // 3 binary value 0011 & 0001 =1
   if (!(n&1))
   return "even number"; // then return "even number
   else
   return "odd number"; // else return "odd number"
}
// implement fact() function with single integer parameter and return long integer
// using recursion
long fact(int n)
{
   if(n==0) return 1; // check n=0 then return 1
   else
   return n*fact(n-1); // else, calling recursively fact(n-1)*n until n=0
}
// implement pow() function with two integer parameters and return long integer
// using recursion
long pow(int n,int power)
{
   if(power<=0) return 1; // check power<=0 then return 1
   return n*pow(n,power-1); // else, calling recursively n*pow(n,power-1) until power=0
}
int main()
{
   int n,power; // Declare two integer numbers n and power
   cout<<"Enter Positive Integer Number: ";
   cin>>n; // return positive integer number
  
   if(n>=0) // check whether n is positive or not, if it positive then,
   {
  
   cout<<n<<" is "<<evenRodd(n)<<endl; // calling evenRodd() function and print result

   cout<<"Factorial of "<<n<<" is: "<<fact(n)<<endl; // calling fact() function and print result
  
   cout<<"\nEnter Power: ";
   cin>>power; // read power value

   cout<<n<<"^"<<power<<" Value: "<<pow(n,power)<<endl; // calling pow() function and print result
}
else
cout<<"Please Enter Positive number..."<<endl; // if n is negative number then display this message
   return 0;
}

OUTPUT-1

Enter Positive Integer Number: 12
12 is even number
Factorial of 12 is: 479001600

Enter Power: 2
12^2 Value: 144

OUTPUT-2

Enter Positive Integer Number: 5
5 is odd number
Factorial of 5 is: 120

Enter Power: 3
5^3 Value: 125

OUTPUT-3

Enter Positive Integer Number: -7
Please Enter Positive number...

UPDATED PROGRAM

#include<iostream>
using namespace std;

// implement evenRodd() function with single integer parameter
// return type string
// and this is implement without using modulus or $
string evenRodd(int n)
{
   // check condition with bitwise & with 1
   // e.g., 2 binary value 0010 & 0001 = 0
   // 3 binary value 0011 & 0001 =1
   if (!(n&1))
   return "even number"; // then return "even number
   else
   return "odd number"; // else return "odd number"
}
// implement fact() function with single integer parameter and return long integer
// using recursion
long fact(int n)
{
   if(n==0) return 1; // check n=0 then return 1
   else
   return n*fact(n-1); // else, calling recursively fact(n-1)*n until n=0
}
// implement pow() function with two integer parameters and return long integer
// using recursion
long long pow(int n,int power)
{
   if(power<=0) return 1; // check power<=0 then return 1
   return n*pow(n,power-1); // else, calling recursively n*pow(n,power-1) until power=0
}
int main()
{
   int n,power; // Declare two integer numbers n and power
   cout<<"Enter Positive Integer Number: ";
   cin>>n; // return positive integer number
  

   cout<<n<<" is "<<evenRodd(n)<<endl; // calling evenRodd() function and print result

   cout<<"Factorial of "<<n<<" is: "<<fact(n)<<endl; // calling fact() function and print result
  
   cout<<"\nEnter Power: ";
   cin>>power; // read power value

   cout<<n<<"^"<<power<<" Value: "<<pow(n,power)<<endl; // calling pow() function and print result

   return 0;
}

LATEST UPDATED PROGRAM

#include<iostream>
using namespace std;

// implement evenRodd() function with single integer parameter
// return type string
// and this is implement without using modulus or $
string evenRodd(int n)
{
   // check condition with bitwise & with 1
   // e.g., 2 binary value 0010 & 0001 = 0
   // 3 binary value 0011 & 0001 =1
   if (!(n&1))
   return "even number"; // then return "even number
   else
   return "odd number"; // else return "odd number"
}
// implement fact() function with single integer parameter and return long integer
// using recursion
long fact(int n)
{
   if(n==0) return 1; // check n=0 then return 1
   else
   return n*fact(n-1); // else, calling recursively fact(n-1)*n until n=0
}
// implement pow() function with two integer parameters and return long integer
// using recursion
long long pow(int n,int power)
{
   if(power<=0) return 1; // check power<=0 then return 1
   return n*pow(n,power-1); // else, calling recursively n*pow(n,power-1) until power=0
}
int main()
{
   int n,power; // Declare two integer numbers n and power
   cout<<"Enter Positive Integer Number: ";
   cin>>n; // return positive integer number
  

   cout<<n<<" is "<<evenRodd(n)<<endl; // calling evenRodd() function and print result

   
   cout<<"\nEnter Power: ";
   cin>>power; // read power value

   cout<<n<<"^"<<power<<" Value: "<<pow(n,power)<<endl; // calling pow() function and print result
if(n>=0){
     
   cout<<"Factorial of "<<n<<" is: "<<fact(n)<<endl; // calling fact() function and print result

}
else
cout<<"Factorial Does not support negative integer"<<endl;

   return 0;
}


Related Solutions

Create a C++ program that will accept any number of grades for an exam. The grades...
Create a C++ program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category. Using arrays.
Create a C++ program which will accept an unlimited number of scores and calculates the average...
Create a C++ program which will accept an unlimited number of scores and calculates the average score. You will also need to prompt for the total points possible. Assume each test is worth 100 points. Requirements. • Enter the Student ID first, then prompt for grades. Keep prompt- ing for grades until the client enters ’calc’. That triggers final pro- cessing. • Make your code as reliable as possible. • Make your program output easy to read. • You cannot...
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
DO IN C++ Secret Codes! Create a program that will accept a message from the user...
DO IN C++ Secret Codes! Create a program that will accept a message from the user and either encrypt ordecrypt it with the following algorithms: To encrypt: - get the character you wish to encrypt - find the index of that character in the alphabet array - add the shift offset to the index - add the increment to the index - "wrap around" the new index so the result is between 0 and 29 - find the character at...
Create a working C# Program that will accept an input and has the following class. areaCircle...
Create a working C# Program that will accept an input and has the following class. areaCircle – computes the area of the circle volumeCube – computes the volume of a cube perimeterTraingle – computes the perimeter of a triangle surfaceAreaRect – computes the surface area of a rectangle *You may usePass by Value and/or Pass by Reference *Using ConsoleApp
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Create a c++ program to compute the product of two integers. call create the following functions:...
Create a c++ program to compute the product of two integers. call create the following functions: 1. getNum - to accept only positive numbers && will call computeProd. 2.computeProd - to compute the product of the numbers & will call the function displayProduct. 3. displayProduct - to display the product. Call the function getNum in the main function. Compute the product w/o using the multiplication operator(*). using #include <iostream> only
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT