DevC++ Help
|
ahoy~ness. D: uhm, devc++... the school is teaching us this thing. xD just the basics i guess... =_____= but anyway, he asked us if how do we know if it's odd or even? y'know.. the if and else statements in devc++.. ^____^" i can figure out how the program will 'know' if the number is negative or positive [by using "n > 0" in the if statement] but i can't quite figure out how it'll 'know' if the number is odd or even. does anybody know how? ^^" sorry. *insert epic picture here* |
Re: DevC++ Help
Link |
by
on 2007-08-28 06:10:11 (edited 2007-08-28 06:13:26)
|
we didn't have C++ (i think); we had java last last year though.. i think they're similar? >< here's a sample prog. we had before~ :o i think it also tells if the number is less than 10 import java.io.*; class IfSampleProg1 { public static void main (String args[]) throws IOException{ BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); String strNum; int num; System.out.print ("Enter a number: "); strNum = stdin.readLine(); System.out.println(); num = Integer.parseInt (strNum); if (num % 2 == 0){ System.out.println (num + " is even"); if (num >= 10) System.out.println (num + " is greater than or equal to 10"); else System.out.println (num + " is less than 10"); } else{ System.out.println (num + " is odd"); if (num >= 10) System.out.println (num + " is greater than or equal to 10"); else System.out.println (num + " is less than 10"); } } } m y . L i F E . i . t r a d e . i n . f o r . y o u r . P A i N . |
Re: DevC++ Help
|
aahhh.. coolness o.o num % 2 == 0? @_@ that's the equation to know if it's odd or even? :o *tries* coolness! D: THANKS RIN!! ^_______^ *insert epic picture here* |
Re: DevC++ Help
Link |
by
on 2007-08-28 06:51:05 (edited 2007-08-28 06:51:49)
|
ahh ooh yeh.. explanation :: % = modulo ?? as in.. the remainder. XD so, if n % 2 == 0 → even =) ^ the remainder of n/2 = 0 ^ lololol~ copy paste lang ata ginawa ko >< gahihihi~ sowri hindi ako marunong magturo =___= ›› np maia maia !!~ ^_^ god bless sa studies!~ ike ike d(^o^)b m y . L i F E . i . t r a d e . i n . f o r . y o u r . P A i N . |
Re: DevC++ Help
|
Using modulus is a very unefficect way of doing that problem. A much better way would be using the "&"(and) bitwise operator. If you look at the numbers in binary, you'll see that for odd numbers the least significant bit is always set, and for even numbers it is never set. ex: 1 = 00000001 2 = 00000010 3 = 00000011 4 = 00000100 5 = 00000101 6 = 00000110 7 = 00000111 8 = 00001000 -----------------^ alternating----| The "&"(and) operator will clear all bits not set in the value it is being and'ed with. ex: bitwise &(and) operation: 1100 1010 ---- 1000 So to apply this to a program, you can do something like this: if(number & 1 == 1) { // is odd } if(number & 1 == 0) { // is even } |
Re: DevC++ Help
Link |
by
on 2007-08-30 07:54:39 (edited 2007-08-30 08:24:08)
|
Math is the solution to your problem. In order to understand what you gonna program, you will understand the main root of the problem, that is "HOW DO WE KNOW IF TE NUMBER IS ODD OR EVEN?" The 3 basic rules to determine if the whole number is odd or even are to... 1. DIVIDE. The numbers that are divisible by 2 are EVEN numbers. The others are ODD numbers because they have remainders. Example: (45 Divided By 2 = 22.5) 45 is ODD, (756 Divided By 2 = 378) 756 is EVEN 2. SEE THE LAST NUMBER: If a whole number's last digit number is ending with 0, 2, 4, 6, and 8, are EVEN numbers, this method also can be use to check if decimal numbers are odd or even. Example Whole Number: (465) 5 is last number so it is ODD, (934) 4 is last number so it is EVEN. Example Decimal Number: (56.75) 5 is last number so it is ODD, (784.8) 8 is the last number so it is EVEN. But 78.10 can be round off to 78.1, so it is ODD. 3. PRIME NUMBERS: All prime numbers are odd, with one exception, the prime number 2 which is divisible by 2. Modulo (MOD) Operator or Bitwise (&) operator is OK to use in determining even or odd numbers in programming. ------------------------------------------- MOD Operator can be like as Rule #1 as pseudo code below. if number mod 2 = 0 then "EVEN" elseif number mod 2 = 1 then "ODD" End Condition ------------------------------------------- But Bitwise (&) operator can also determine by that Rule #1 also, but a little coding differences if number & 1 = 0 then "EVEN" elseif number & 1 = 1 then "ODD" End Condition ------------------------------------------- The Rule #2 is like this pseudo code. Unlike Rule #1, this is one of the effective method in identify whole and decimal numbers if it is odd or even... if lastnumber = 2 or 4 or 6 or 8 or 0 then "EVEN" elseif lastnumber = 1 or 3 or 5 or 7 or 9 then "ODD" End Condition ------------------------------------------- Whatever Methods, Argorithms, or Procedures you use in Programming, it is OK. But the best is you must understand what you intend to program to. If you can't understand what you gonna program, do it in manual way before you automize it. |