class PrimeNo {
static int i = 2;
public static boolean isPrime(int ip) {
if (i < ip) {
if (ip % i == 0) {
//system.out.println(i); // it is giving 3
i = 2;
return false;
}
i++;
isPrime(ip);
}
i = 2;
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(9));
}
}
Why it is returning true not false? As I have incremented i++ when inner if is not executed. When inner if executes then it should return false. why It is skipping.
Classical mistake: calling isPrime(ip) alone does not cause a return.
Related
I do not understand what I should return. My method returns false if the last time it goes
through the for-loop it is false. If the last time is true than it returns true. But I want it to return false regardless of where the false occurred.
public class test {
public static void main(String[] args) {
int number = 4;
int[] intArray = {4, 8, 12, 16};
System.out.println(allMultipleOf(intArray, number));
}
public static boolean allMultipleOf(int[] ary, int n){
boolean a = true;
for(int i = 0; i < ary.length; i++){
if(ary[i] % n == 0){
a = true;
//System.out.println(a);
break;
} else {
a = false;
}
}
}
return a; //what should I return
}
You can return false early from the method, if we reached the end without returning false, then return true:
public static boolean allMultipleOf(int[] ary, int n) {
for (int i = 0; i < ary.length; i++) {
if (ary[i] % n != 0) {
return false;
}
}
return true;
}
It should return false, the default of the boolean is print at this place.
You can make the return statement within the method. it return true.
public static boolean allMultipleOf(int[] ary, int n) {
boolean a = true;
for(int i = 0; i < ary.length; i++) {
if (ary[i] % n == 0) {
a = true;
//System.out.println(a);
break;
} else {
a = false;
}
}
return a;
}
Why does return statement is throwing error when used Math function in a method.
public class HCF_1 {
static int hcf(int a, int b)
{
int res = Math.max(a,b);
while(true)
{
if(res%a==0 && res%b==0)
return res;
else res++;
}
return res;
}
public static void main(String[] args) {
System.out.println(hcf(5,25));
}
}
This may or may not be helpful, but IMO while(true) statements are a real code smell. You can rewrite this method as:
public class HCF_1 {
static int hcf(int a, int b)
{
int res = Math.max(a,b);
while(res % a != 0 || res % b != 0)
res++;
return res;
}
public static void main(String[] args) {
System.out.println(hcf(5,25));
}
}
Now there is only a single return statement, and no shortcuts.
Note that the operations !(res % a == 0 && res % b == 0) are the same as res % a != 0 || res % b != 0, due to to the properties of Boolean Logic: ~(A AND B) == ~A OR ~B.
public class HCF_1 {
static int hcf(int a, int b)
{
int res = Math.max(a,b);
while(true)
{
if(res%a==0 && res%b==0)
return res;
else res++;
}
return res; //last line of the method hcf is unreachable
}
public static void main(String[] args) {
System.out.println(hcf(5,25));
}
}
The while loop is a never-ending loop and is escaped only under the condition mentioned inside if block which is a return statement and not a break statement. Therefore the last line of the method hcf return res; is unreachable in any condition.
the segement of your code in if-else the cause to be unreachable for the last line of your return res, you so have to do 2 things:
remove the return inside if and add break instead.
return the last line of method which is return res;
public class HCF_1 {
static int hcf(int a, int b) {
int res = Math.max(a, b);
while (true) {
if (res % a == 0 && res % b == 0)
break;
else
res++;
}
return res;
}
public static void main(String[] args) {
System.out.println(hcf(5, 25));
}
}
When iterate() is first called i'm generating a random number between 1-1024 and then checking if that random number is equal to 1. If it is, then done = true and the return value should be 10. If it's not, then the value of x should be decremented (x now = 9). I then expect getRandNum() to be called again within iterate(), this time generating a random number between 1-512 and checking if it's equal to 1 etc, etc... until the return value for x can only be equal to 1.
import java.util.Random;
public class Main {
static boolean done = false;
public static int rand(int x) {
Random rand = new Random();
return rand.nextInt(x) + 1;
}
public static int value(int x) {
return (int) Math.pow(2, x);
}
public static int getRandNum(int x) {
return rand(value(x));
}
public static int iterate() {
int x = 10;
do {
if (getRandNum(x) == 1) {
done = true;
}
else {
if (x > 1) {
x--;
}
else {
x = 1;
done = true;
}
}
} while (done = false);
return x;
}
public static void main(String[] args) {
System.out.println(iterate());
}
}
Right now iterate() only returns 9. I need it to return anything from 1-10. There's clearly a logical error, but i can't see it.
Small error: you did while(done = false). This assigns done to false. The correct way to do this is while(!done).
public static int iterate() {
int x = 10;
do {
if (getRandNum(x) == 1) {
done = true;
}
else {
if (x > 1) {
x--;
}
else {
x = 1;
done = true;
}
}
} while (!done);
return x;
}
If you do this it prints 1.
So here's my code, I want the output to be like this:
Given two numbers, is the second input a multiple of the first?
For Example:
Input:
3
6
Output:
true
public boolean multiple(int m, int n){
int i = 0;
int j = 0;
boolean check = true;
if(n%m == 0){
i++;
return check;
}
else{
j++;
return false;
}
}
When I try it I get an error, I think it's because the return statement is within the if and else statements.
The code is perfectly fine .. Error must be Somewhere else
public class Test1 {
public static void main(String[] args) {
System.out.println(multiple(3, 9));
}
public static boolean multiple(int m, int n){
int i = 0;
int j = 0;
boolean check = true;
if(n%m == 0){
i++;
return check;
}
else{
j++;
return false;
}
}
}
Output
true
here is output see IDEONE
The easiest way is to return the result of your if statement.
return n % m == 0;
I'm not sure what i/j are doing. You don't use them except to increment, but they are local to the function and get GC'd after the return. What you have now is basically this:
boolean bool = some_calculation();
if (bool == true)
{
return true;
}
else
{
return false;
}
Im quite the novice when it comes to programming and im trying to translate this PHP algorithm to Java.
function isPrime($n)
{
$i = 2;
if ($n == 2) {
return true;
}
while ($i < $n) {
if ($n % $i == 0) {
return false;
}
$i++;
}
return true;
}
for ($i = 3; $i < 100; $i++) {
if (isPrime($i)) {
echo $i;
}
}
The only thing i've come up with so far is this.
public class Primtal {
public static boolean isPrime(int n)
{
int i = 2;
if (n == 2) {
return true;
}
while (i < n) {
if ( n % i == 0) {
return false;
}
i++;
}
return true;
}
for(int i = 3; i < 1000; i++){
if (isPrime(i)) {
System.out.print(i);
}
}
}
I realize this look really stupid but i really need to get this to work. I think the problem lies mostly with the for loop as im currently getting the error illegal start of type there. Im not really sure how to translate this to Java and i would appreciate any help i can get.
I believe the problem with your code is that you've put a for loop in the middle of class declaration, which is incorrect - it needs to be inside some method. It seems logical in this case to put it in main(), so it's executed when you run your program. Maybe something like this:
public class Primtal
{
public static boolean isPrime(int n)
{
int i = 2;
if(n == 2)
{
return true;
}
while(i < n)
{
if(n % i == 0)
{
return false;
}
i++;
}
return true;
}
public static void main(String[] args)
{
for(int i = 3; i < 1000; i++)
{
if(isPrime(i))
{
System.out.print(i);
}
}
}
}
(Note the addition of public static void main(String[] args) in the second half of the code.)
Oracle has official tutorials on how Java programs need to be structured, and other basics of the language. You can find the one related to the main method here. Or, to start from the beginning, the full tutorial starts here.
you can't write the for loop
for(int i = 3; i < 1000; i++){
if (isPrime(i)) {
System.out.print(i);
}
}
directly inside a class.
i believe what you wish to do is to have a main method, in which you can have the for loop
Your for loop needs to be within a method of some sort,so you can put it in the main method:
public class Primtal {
public static void main(String [] args)
{
for(int i = 3; i < 1000; i++)
{
if (isPrime(i)) {
System.out.print(i);
}
}
public static boolean isPrime(int n)
{
int i = 2;
if (n == 2) {
return true;
}
while (i < n) {
if ( n % i == 0) {
return false;
}
i++;
}
return true;
}
}
The problem is that your for loop isn't in a method. Enclose it in a main method.
public static void main(String[] args) {
// Your for loop here
}
Also, change print to println, or else all the numbers will appear concatenated together on one line.