Construct a program that would prompt the user for an integer. Include a function in your program that would evaluate whether the integer is negative or positive. aslo include another function that would determine whether the integer is odd or even.
Here is my code. But I not sure whether I my code is following the question stated above. Anyone could tell me my mistakes and advices ? Thank you.
#include<iostream>
#include<iostream>
int pnoe(int);
usingnamespace std;
int main()
{
int number;
cout<<"Please enter a number : ";
cin>>number;
pnoe(number);
system("pause");
return 0;
}
int pnoe(int number)
{
if(number > 0 )
{
cout<<number<<" is a positive number"<<endl;
}
elseif(number < 0)
{
cout<<number<<" is a negative number"<<endl;
}
if(number%2==0)
{
cout<<number<<" is an even number"<<endl;
}
else
{
cout<<number<<" is an odd number"<<endl;
}
return number;
}
bool IsPositiveOrNegative (int I) // might return true if I is positive else it might return negative.
bool isOddOrEven (int I)// might return true if I is even or false if odd.
Well, your code returns the required information, but I think the original question asks for two separate functions, probably returning a bool (true or false).
Maybe two prototypes:
1 2
bool isEven( int number ); // even returns true; odd returns false
bool isPositive( int number ); // positive returns true; non-positive returns false;
The latter is complicated by the fact that a number can be positive, negative or zero. You would have to choose what you want your function to do in the last case.
#include<iostream>
#include<iostream>
int pn(int);
int oe(int);
usingnamespace std;
int main()
{
int number;
cout<<"Please enter a number : ";
cin>>number;
pn(number);
oe(number);
system("pause");
return 0;
}
int pn(int number)
{
if(number > 0 )
{
cout<<number<<" is a positive number"<<endl;
}
elseif(number < 0)
{
cout<<number<<" is a negative number"<<endl;
}
return number;
}
int oe(int number)
{
if(number%2==0)
{
cout<<number<<" is an even number"<<endl;
}
else
{
cout<<number<<" is an odd number"<<endl;
}
return number;
}
I don't see the need to return the number - it's just the same as on entry. I should just return a bool (true or false).
Also, functions like this should really only do one thing: yours are doing TWO: deciding even or odd AND printing out some information. The user needed the first, but he isn't being given any choice about the second: that would be better done in main().
The following does just the even/odd test. Note that (a) the function is extremely short; (b) it does one thing; (c) the decision whether to output anything is left to whatever called that function (in this case, from inside main()).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
bool isEven( int number ) { return (number%2==0); }
int main()
{
int number;
cout << "Please enter a number : ";
cin >> number;
if ( isEven( number ) ) cout << "Number is even" << endl;
else cout << "Number is odd" << endl;
// system( "pause" );
}
#include <iostream>
#include <string>
// Include a function in your program that would evaluate
// whether the integer is negative or positive.
std::string sign_of_integer( constint);
// Also include another function that would determine
// whether the integer is odd or even.
bool is_even( constint);
int main()
{
// Construct a program that would prompt the user for an integer.
int an_integer = 0;
std::cout << "Please enter an integer: ";
std::cin >> an_integer;
std::cout << an_integer << sign_of_integer( an_integer) << '\n';
if ( is_even(an_integer) )
std::cout << an_integer << " is even.\n";
else
std::cout << an_integer << " is odd.\n";
return 0;
}
// Include a function in your program that would evaluate
// whether the integer is negative or positive.
std::string sign_of_integer( constint number)
{
if(number > 0)
return" is positive.";
elseif( number == 0)
return" is zero.";
elsereturn" is negative.";
}
// function that would determine
// whether the integer is odd or even.
bool is_even( constint number)
{
if(number % 2 == 0)
returntrue;
elsereturnfalse;
}
@Chervil @kemort okay. I will try it out again.
Can I know which version is the actual code and structure which mean more sufficient toward the questions above?