Function oddeven & positivenegative integer

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<iostream>
int pnoe(int);
using namespace 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;
	}
	else if(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;
}



	
Last edited on
closed account (SECMoG1T)
You need two functions as the question states
1
2
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. 


Just a suggestion.
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.
Last edited on
@andy1992 @lastchance thanks for the reply.

By any chance, do I meet the requirement for the question by doing this structure?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<iostream>
int pn(int);
int oe(int);
using namespace 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;
	}
	else if(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>
using namespace 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" );
}
@lastchance. Okay. Is this the correct way ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<iostream>
bool isPositive(int number);
bool isEven(int number);
using namespace std;

bool isPositive(int number)
{
	return(number > 0 );
}
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;
   }
	if ( isPositive( number ) ) 
	{
		cout << "Number is Positive" << endl;
	}
   else  
   {                  
	   cout << "Number is Negative"  << endl;
   }
	system("pause");
	return 0;
}

Last edited on
That version would state that zero is negative.
closed account (48T7M4Gy)
One way to check against the original question is to incorporate it in the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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( const int);

// Also include another function that would determine
// whether the integer is odd or even.
bool is_even( const int);

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( const int number)
{
    if(number > 0)
        return " is positive.";
    else if( number == 0)
        return " is zero.";
    else
        return " is negative.";
}

// function that would determine
// whether the integer is odd or even.
bool is_even( const int number)
{
    if(number % 2 == 0)
        return true;
    else
        return false;
}
Last edited on
@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?
Last edited on
Topic archived. No new replies allowed.