Function to determine even or odd

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2438 Views - Last Post: 07 May 2009 - 08:05 AM Rate Topic: -----

#1 aiy000  Icon User is offline

  • New D.I.C Head

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

Function to determine even or odd

Post icon  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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 aiy000  Icon User is offline

  • New D.I.C Head

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

Re: Function to determine even or odd

Posted 04 May 2009 - 08:35 AM

View PostBlakeJustBlake, 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
Was This Post Helpful? 0
  • +
  • -

#4 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#6 IngeniousHax  Icon User is offline

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

Reputation: 78
  • View blog
  • Posts: 1,358
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Function to determine even or odd

Posted 05 May 2009 - 01:01 AM

View PostIngeniousHax, 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!!
Was This Post Helpful? 0
  • +
  • -

#8 aiy000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 vipercaleb1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}



:rolleyes: :D

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

Was This Post Helpful? 0
  • +
  • -

#10 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • 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;
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,071
  • 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?
Was This Post Helpful? 0
  • +
  • -

#13 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Function to determine even or odd

Posted 06 May 2009 - 01:25 AM

View Postno2pencil, 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.
Was This Post Helpful? 0
  • +
  • -

#14 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • 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; }


Was This Post Helpful? 0
  • +
  • -

#15 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • 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 :wink:

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

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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2