# 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();
}
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.
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.)
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.
#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;
}
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"
}
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.
int var;
...
if (var & 1) printf("%d is odd\n", var);
else printf("%d is even\n", var);
int isEven (int x) return x % 2 == 0 ? 1 : 0;
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 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
#include
1. Read in 'n'2. Output n*(n+1)/2> Check the no. in odd or even?Both possible, has no significance.
#include <stdio.h> 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",&n); if( n 0) { printf("%d is a EVEN number.\n", n); } else { printf("%d is a ODD number.\n", n); } } return 0; }
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 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
#include <stdio.h> main() { int n, odd=0, even=0; while (scanf("%d",&n)&&(n!=0)) (n%2)?++odd:++even; printf("odd: %d even: %d\n",odd,even); }
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 & 1), which would be true for an odd number, false for even
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.
odd loop means at least the loop execute once.
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
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.
#include
1. Read in 'n'2. Output n*(n+1)/2> Check the no. in odd or even?Both possible, has no significance.
#include <stdio.h> 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",&n); if( n 0) { printf("%d is a EVEN number.\n", n); } else { printf("%d is a ODD number.\n", n); } } return 0; }
Even (unless c = 0 in which case it is either or both!)