English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i wanna write a program in borland c language when the user write a even number the program print this number is even and if else else print to the user this number is odd how i can make the computer characterize which is even and which is odd

2006-06-12 11:16:45 · 3 answers · asked by marshallmedo 2 in Computers & Internet Programming & Design

3 answers

Use the modulo (%) character. The modulo is like a division character but it only returns a 0 or 1.

0=no remainder
1=remainder

Example:
input: 67
------------------------------------
number = 67;

if((67%2)=0){
printf("the number %d is even", number);
}
else{
printf("the number %d is odd", number);
}

In the above example, when you express (67%2) this returns a 1 since there is a remainder. Therefore, 1=0 is a false statement and the program will print the line in the else block.

2006-06-12 11:39:34 · answer #1 · answered by Anonymous · 0 0

very simple...two ways to find it out...

you can take the mod % of the entered number by 2
if the answer is 1 then its an odd number, else its an even number

secondly you can divide the entered number with 2

if the result is integer...the number is even, if the result is float, the result is odd

2006-06-12 11:23:42 · answer #2 · answered by Haseeb Uddin 2 · 0 0

just another unique way would be:

int number;
//get input
if (number & 0x1)
printf("number is odd");
else
printf("number is even");

2006-06-13 01:57:15 · answer #3 · answered by justme 7 · 0 0

fedest.com, questions and answers