![]() |
| | #1 |
| Registered User Join Date: Nov 2002
Posts: 11
| Code: /* a function that tests whether an integer is even, returning a true or false
answer */
#include <iostream>
using namespace std;
int main()
{
int n = 0;
cout << "Enter a number: ";
cin >> n;
cout << test(n) << endl;
return 0;
}
int test(int n)
{
if (n
|
| giggsy is offline |
| | #2 |
| Banned Join Date: Sep 2002
Posts: 6,334
| mod by two. variable = variable % 2; |
| RoD is offline |
| | #3 |
| Code Goddess Join Date: Sep 2001
Posts: 9,661
| Code: #include <iostream>
int main()
{
int val;
std::cout<<"Enter a number: ";
std::cin>> val;
if ( val % 2 == 0 )
std::cout<< val <<" is even"<<std::endl;
else
std::cout<< val <<" is odd"<<std::endl;
}
__________________ My best code is written with the delete key. |
| Prelude is offline |
| | #4 |
| Programming Sex-God Join Date: Nov 2002
Posts: 1,078
| Or, if you want to optimize just do: Value & 1 if the result of the expression is true then the number was odd, if false, then the number was even |
| Polymorphic OOP is offline |
| | #5 |
| Banned Join Date: Sep 2002
Posts: 6,334
| hmm, both seem about the same imo, i had only ever learned the modulous way of doing it. |
| RoD is offline |
| | #6 |
| Programming Sex-God Join Date: Nov 2002
Posts: 1,078
| Bitwise operations are faster than division, multiplication, addition, subtraction because it's just a quick check on the bits. |
| Polymorphic OOP is offline |
| | #7 |
| Banned Join Date: Sep 2002
Posts: 6,334
| thnx for the knowledge. |
| RoD is offline |
| | #8 |
| Registered User Join Date: Nov 2002
Posts: 11
| I tried the following: Code: /* a function that tests whether an integer is even, returning a true or false
answer */
#include <iostream>
using namespace std;
int evenTest(int);
int main()
{
int n = 0;
cout << "Enter a number: ";
cin >> n;
cout << evenTest(n) << endl;
return 0;
}
int evenTest(int n)
{
if (n % 2 == 0)
cout << n << " is even" << endl;
else
cout << n << " is odd" << endl;
}
Code: Enter a number: 10 10 is even 98476 Also, Polymorphic OOP, how would I implement Value & 1 ? |
| giggsy is offline |
| | #9 |
| Programming Sex-God Join Date: Nov 2002
Posts: 1,078
| It's result is 1 if it's odd and 0 if it's even so Code: std::cout << "Value is " << ( Value & 1 ? "odd" : "even" ); |
| Polymorphic OOP is offline |
| | #10 | |
| Programming Sex-God Join Date: Nov 2002
Posts: 1,078
| Quote:
What you should do is: Code: /* a function that tests whether an integer is even, returning a true or false
answer */
#include <iostream>
using namespace std;
bool evenTest(int);
int main()
{
int n = 0;
cout << "Enter a number: ";
cin >> n;
cout << n << " is " << ( evenTest(n) ? "even" : "odd" ) << endl;
return 0;
}
inline bool evenTest(int n)
{
return n % 2 == 0;
}
| |
| Polymorphic OOP is offline |
| | #11 | |
| I lurk Join Date: Aug 2002
Posts: 1,361
| Quote:
| |
| Eibro is offline |
| | #12 |
| Skunkmeister Join Date: Aug 2001
Posts: 2,572
| Code: const char* EvenOrOdd( int value)
{
if(value&1) return"odd";
return "even";
}
__________________ Free the weed!! Class B to class C is not good enough!! And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi |
| Stoned_Coder is offline |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Looking for constructive criticism | wd_kendrick | C Programming | 16 | 05-28-2008 09:42 AM |
| No Match For Operator+ ??????? | Paul22000 | C++ Programming | 24 | 05-14-2008 10:53 AM |
| My C++ test is COMING!!... | [Z-D] | C++ Programming | 52 | 12-01-2006 08:02 PM |
| FAQ how do i convert a string to an integer?? (C) | Unregistered | FAQ Board | 1 | 12-02-2001 05:03 PM |