answersLogoWhite

0

C program for odd or even?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

# include

void main()

{

int a;

printf("\n Enter a number to find if its odd or even");

scanf("%d",&a);

if(a%2==0)

{

printf("\n The number entered is even");

}

else

{

printf("\n The number entered is odd");

}

getch();

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Write a program that inputs a series of integers and passes them one at a time to a function even which uses the remainder operator to determine if an integer is even. The function should take an integer argument and return 1 if the integer is even and 0 otherwise.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Add one to (a copy of) the number and then take the integer of the result. If the two values are the same, then the original number was even. (This only works correctly for integers.)

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

I will not write a program for you submit as your homework.

A integer number can checked for odd or even by looking at the low order bit. If the low order bit is 1 (true) the number is odd.

The rest is up to you.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include

using std::cin;
using std::cout;

int main()
{
int num = 0;
cout << endl << "Enter a number: ";
cin >> num;

if (num % 2 == 0)
{
cout << endl << "The number " << num << " is even";

}
else
{
cout << endl << "The number " << num << " is odd";

}

system("PAUSE");
return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Considering you already got the number input from keyboard or file, and "int a" will be your number.

if (a/2 == 0)

{

cout << a << "is even"

}

else

{

cout << a << "is odd"

}

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

When we divide any whole number (integer) by 2, the remainder can only be 0 or 1. If the remainder is 0 then the number must be even, otherwise it must be odd. Given that 0 and 1 implicitly convert to the Boolean values false and true, respectively, we can easily implement the required functions as follows, using the modulo operator (%) to determine the remainder after integer division by 2:

bool is_odd (int num) { return num%2; } // means: (num%2)==1
bool is_even (int num) { return !is_odd (num); } // means: (num%2)!=1 or (num%2)==0;

Note that for unsigned integers, we can improve efficiency further by testing the low-order (least-significant) bit using the bitwise logical AND operator (&):

bool is_odd (unsigned num) { return num&1; } // means: num&1==1;
bool is_even (unsigned num) { return !is_odd(num); } // means: (num&1)!=1 or (num&1)==0;

This works because the least-significant bit of an unsigned integer represents the value 2^0 which is 1 (hence we test for 1), while all the higher-order bits represent increasing powers of 2 which are 2, 4, 8, etc, all of which are even. So, regardless of which other bits are set (or not), the low-order bit alone tells us if the number is odd or even. If it is set, the number is odd, otherwise it is even.

Although bitwise logic is much faster than modulo integer division, it is not guaranteed to be portable for all signed integers given that some (older) systems represent negative integers using ones-complement notation while most (newer) systems use twos-complement notation, thus complicating the logic. The first solution shown above is guaranteed to be portable across all systems whereas testing for and converting negative values to positive values to take advantage of the bitwise logic in the second solution would effectively negate that advantage. Therefore use the second solution only when all integers to be tested are guaranteed to be unsigned and therefore do not require conversion, or if the systems are guaranteed to be twos-complement systems.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

int var;
...
if (var & 1) printf("%d is odd\n", var);
else printf("%d is even\n", var);

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

int isEven (int x) return x % 2 == 0 ? 1 : 0;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program for odd or even?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a C program to check whether the number is odd or even Ps-using DEV complier?

To write a C program to determine if something is odd or even you need to be a programmer. To write a program in C is complicate and only done by programmers.


Write a program in c that prompt user with following lines add two integers test an integer for odd or even and quit?

write a program in C that prompts the user with the following lines: a) Add two integers c) Compare two integers for the larger t) Test an integers for odd or even q) Quit


C program to accept a sequence of number terminated by zero and sum all odd nos and even nos?

#include &lt;stdio.h&gt; main() { int n, odd=0, even=0; while (scanf("%d",&amp;n)&amp;&amp;(n!=0)) (n%2)?++odd:++even; printf("odd: %d even: %d\n",odd,even); }


Write c program to find odd or even no?

For any number n you could use * (n % 2 == 0), which would be true for an even number, false for odd For an integer i, a simpler method would be * (i &amp; 1), which would be true for an odd number, false for even


If n is an odd integer is n plus 6 odd as well?

Yes. If n is odd, then n + c where c is an even constant will be odd. n + d where d is an odd constant will be even.


What is odd loop in 'C' program?

odd loop means at least the loop execute once.


is 986 even or odd?

Even b/c if the last number is even then even and if the last number is odd then the number is odd so 986 _6_ is even


Can 5 odd numbers make 50?

No. Two odd numbers added together always give an even number. Two even numbers added together always give an even number. An odd and an even number added together always give an odd number. So, if we have 5 odd numbers - a, b, c, d and e a + b will be even c + d will be even So if (a + b) + (c + d) is even, adding e to that will have to be an odd number - but 50 is even, so it cannot be done.


Write a C Program to print sum of squares of odd numbers?

#include


Could u draw me a flowchart for this C program question 1.sum of nos. from 1 to n Check the no. in odd or even?

1. Read in 'n'2. Output n*(n+1)/2> Check the no. in odd or even?Both possible, has no significance.


Turbo c program that will determine if the input is odd or even number?

#include &lt;stdio.h&gt; int main() { printf("Program to find ODD or Even Number\n"); while(1) { int n = 0; printf("\nEnter a number(-1 for Exit): "); scanf("%d",&amp;n); if( n 0) { printf("%d is a EVEN number.\n", n); } else { printf("%d is a ODD number.\n", n); } } return 0; }


Is fx equals c an even or odd function?

Even (unless c = 0 in which case it is either or both!)