# Function to determine even or odd

• (2 Pages)
• 1
• 2

## 19 Replies - 3496 Views - Last Post: 07 May 2009 - 08:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=103273&amp;s=ffa387662cbd6465bb56051c09cfb4f4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aiy000

Reputation: 0
• Posts: 16
• Joined: 04-May 09

# Function to determine even or odd

Posted 04 May 2009 - 07:31 AM

Hello everybody i am a new programmer.
1- i want to write a function that can return for me 1 if the number is odd and returns 0 otherwise.
2- how i can call this function to know if this number entered is even or odd.
I started with this code can anybody help me please:
```#include <stdio.h>
int isOdd (int x);
int main (void)
{
int x=0;
printf("Enter a positive number:");
scanf("%d",&x);
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Function to determine even or odd

### #2 BlakeJustBlake

• D.I.C Regular

Reputation: 26
• Posts: 441
• Joined: 15-February 09

## Re: Function to determine even or odd

Posted 04 May 2009 - 07:42 AM

Well, you should take the declaration out of the function prototype (the way you wrote the function on top of the main method) and since you want it to be only 1 or 0 then it can just return a boolean value: bool isOdd(int);

Then, below the main method you can write the implementation, which should be in this form:

```bool isOdd(int x) {
//write check for if it's odd or not.
}

```

Then to call it in your main method you just write something like this:

```int main (void)
{
int x=0;
printf("Enter a positive number:");
scanf("%d",&x);
if(isOdd(x)) {
printf("It's odd.\n");
}
else {
printf("It's even.\n");
}
return 0;
}

```

This post has been edited by BlakeJustBlake: 04 May 2009 - 07:42 AM

### #3 aiy000

Reputation: 0
• Posts: 16
• Joined: 04-May 09

## Re: Function to determine even or odd

Posted 04 May 2009 - 08:35 AM

BlakeJustBlake, on 4 May, 2009 - 06:42 AM, said:

Well, you should take the declaration out of the function prototype (the way you wrote the function on top of the main method) and since you want it to be only 1 or 0 then it can just return a boolean value: bool isOdd(int);

Then, below the main method you can write the implementation, which should be in this form:

```bool isOdd(int x) {
//write check for if it's odd or not.
}

```

Then to call it in your main method you just write something like this:

```int main (void)
{
int x=0;
printf("Enter a positive number:");
scanf("%d",&x);
if(isOdd(x)) {
printf("It's odd.\n");
}
else {
printf("It's even.\n");
}
return 0;
}

```

sorry BlakeJustBlake i didn't understand what you said i deleted the declaration prototype but when i wrote the bool value it gaves me an error and also how can i check by this code
```bool isOdd(int x) {
//write check for if it's odd or not.
}

```

can you please be more detailed

### #4 BlakeJustBlake

• D.I.C Regular

Reputation: 26
• Posts: 441
• Joined: 15-February 09

## Re: Function to determine even or odd

Posted 04 May 2009 - 09:05 AM

You can't check by that code, you have to write your own code, I'm just trying to help you understand how functions work.

### #5 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Function to determine even or odd

Posted 04 May 2009 - 01:03 PM

BlakJustBlake meant that you write the function prototypes before the functions(s) that use them i.e. int main(void) in this case and then write the full functions themselves elsewhere in your source-code. This is the sort of thing you need:

```#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

bool isOdd(int x);
bool isNumeric(string s);

int main(void)
{
cout << "Enter a positive number:";
string sBuf;
cin >> sBuf;

if (isNumeric(sBuf))
{
int n = atoi(sBuf.c_str());

if (isOdd(n))
{
cout << n << " is an odd number" << endl;
}
else
{
cout << n << " is an even number" << endl;
}
}
else
{
cout << "You didn't enter a number!";
}

return 0;
}

bool isOdd(int x)
{
bool bRet = false;

// TO DO - write code to perform required function

return bRet;
}

bool isNumeric(string s)
{
for (int n = 0; n < s.length(); ++n)
{
char c = s.at(n);

if (c < '0' || c > '9')
{
return false;
}
}

return true;
}

```

### #6 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

## Re: Function to determine even or odd

Posted 04 May 2009 - 04:33 PM

if(num % 2 == 0) I think... yeah even numbers divide into two all the time.

### #7 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Function to determine even or odd

Posted 05 May 2009 - 01:01 AM

IngeniousHax, on 4 May, 2009 - 10:33 PM, said:

if(num % 2 == 0) I think... yeah even numbers divide into two all the time.

Yes, well "we can do it" but it was left for the person who asked the question to work that one out. Duh!!

### #8 aiy000

Reputation: 0
• Posts: 16
• Joined: 04-May 09

## Re: Function to determine even or odd

Posted 05 May 2009 - 04:49 AM

Thank you all for your help and it is [b]helpful for me now thanks.

### #9 vipercaleb1

Reputation: 0
• Posts: 19
• Joined: 05-May 09

## Re: Function to determine even or odd

Posted 05 May 2009 - 07:04 PM

Well I guess its allready answered but this is what I did:

```
// fun script to see if an int is odd or even
#include <iostream>
using namespace std;

void odd (int a);
void even (int a);

int main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}

void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}

void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}

```

This post has been edited by vipercaleb1: 05 May 2009 - 07:05 PM

### #10 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Function to determine even or odd

Posted 06 May 2009 - 12:49 AM

That's quite a clean solution. But I think you could improve it by having one fucntion instead of the two:

```void oddOrEven(int n)
{
if (n % 2) == 0))
{
cout << "Number is even" << endl;
}
else
{
cout << "Number is odd" << endl;
}
}

```

### #11 computerfox

• straight vegetarian kid

Reputation: 50
• Posts: 3,772
• Joined: 29-January 09

## Re: Function to determine even or odd

Posted 06 May 2009 - 01:12 AM

```int even(int num){

int even;
if(num%2==0)
even=1;

else
if(num%2!=0&&num>0)
even=0;

return even;
}

```

hope that helps

This post has been edited by computerfox: 06 May 2009 - 01:13 AM

### #12 no2pencil

• Professor Snuggly Pants

Reputation: 6611
• Posts: 30,823
• Joined: 10-May 07

## Re: Function to determine even or odd

Posted 06 May 2009 - 01:20 AM

Since you've already decided that the given number is divisible by 2 in the conditional if, why test it again in the else?

### #13 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: Function to determine even or odd

Posted 06 May 2009 - 01:25 AM

no2pencil, on 6 May, 2009 - 07:20 AM, said:

Since you've already decided that the given number is divisible by 2 in the conditional if, why test it again in the else?

Exactly, that's why I proposed my solution.

### #14 BlakeJustBlake

• D.I.C Regular

Reputation: 26
• Posts: 441
• Joined: 15-February 09

## Re: Function to determine even or odd

Posted 06 May 2009 - 02:19 AM

here's mine:

```bool isOdd(int n) { return n%2 != 0; }

```

### #15 computerfox

• straight vegetarian kid

Reputation: 50
• Posts: 3,772
• Joined: 29-January 09

## Re: Function to determine even or odd

Posted 06 May 2009 - 07:32 AM

Quote

Since you've already decided that the given number is divisible by 2 in the conditional if, why test it again in the else?

just making sure that's it's 1 or 0 and not 1 and 0, but the point was :

```if(num%2=0)
even=1;

else
even=0;

return even;

```

since he wants to use 1 and 0 to decide if it's even or not

it's the same thing as a bool, cept more lines

This post has been edited by computerfox: 06 May 2009 - 07:34 AM