In: Computer Science
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>
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;
}