Method in Java that returns the first odd found in a range -
i need create method receives int n
, tests integers between n*n+1
, (n+1)*(n+1)-1
, returns first odd number encounters. if no such number found between these bounds function return 0
.
i'm trying figure out how while , don't it. i'm quite new java.
can me?
i thought in eclipse says me this method must return result of type int
, didn't understand why.
public static int test(int n){ // receives argument n int = n*n+1; // calculate lower bound int b = (n+1)*(n+1)-1; // calculate upper bound { (int = 1; <=a; i++){ if (a % == 0){ return a; } else if (a % != 0){ return 0; } } ++a; } while (a <= b); }
to check whether number odd, use modulo:
n % 2 == 0
if above condition evaluates true
, number even. evaluate true if number odd:
n % 2 != 0
i hope helps.
Comments
Post a Comment