1) what is the algorithm to check whether an entered number is odd / even using C programming

1) what is the algorithm to check whether an entered number is odd / even using C programming
agcpsAsked:
Who is Participating?
 
NorushConnect With a Mentor Commented:
http://www.cprogramming.com/tutorial/modulus.html
#include <iostream>
 
using namespace std;
 
int main()
{
    int num;
    cin >> num;
    // num % 2 computes the remainder when num is divided by 2
    if ( num % 2 == 0 )
    {
        cout << num << " is even ";
    }
 
    return 0;
}

Open in new window

0
 
Peter HartCommented:
I'm sure you'll get the anwser once the C-heads have woken up but until then....
in words: (my C is rusty)...

if (number modulus 2) equals 1 then
number is odd
else
number is even
end if

heres a gues (may be a mixture of java,C,c++)...
isodd (int number) bool
{
if   (  (number ^2) == 1) then
  return true;
else
 return (false);
end if
}
0
 
divyeshhdoshiCommented:
1) Input a no.
2) If a no. is divided by zero then return Even
3) If a no. is not devided by zero then return Odd.
4) End

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
NorushCommented:
You use the modules function for this:
The next line does the actual trick:

if ( num % 2 == 0 )
0
 
Peter HartCommented:
yes its % not ^ that I used


so its really....
bool Isodd (int number)

{
if   (  (number % ) == 1) then
  return (1);
else
 return (0);
end if
retuen 0;
}
0
 
NorushCommented:
agcps, did you find your answer?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.