i'm new java , i've acquired programming experience on past year python , c++; however, find java lot more confusing. trying here determine whether or not number (user input) prime or not through use of loops. here code have far:
public static boolean isprime(int x) { boolean prime = true; if (x % 2 == 0) prime = false; if (x % 2 != 0) { (int = 3; i*i <= x; i+=2) { if (x % == 0) prime = false; } } return prime; }
i've been stuck on quite time , if me figure out, that'd great.
your solution returns true 1 , false 2
public static boolean isprime(int x) { if(x<2) return false; if(x==2) return true; // boolean prime = true; dont need variavle if (x % 2 == 0) return false; // using return faster dont need more conditions // also, dont need if. use else, if dont want return // (int = 3; i*i <= x; i+=2) { if (x % == 0) return false; } return true; }
Comments
Post a Comment