Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been working on this code to check whether no is prime or not the problem is i cannot understand syntax plz help
I am using this code to find whether a no is prime or not the problem is in the for loop i cannot understand why we need to us this condn statment (i<=num/i). Please i don't have problem in syntax i have problem in understanding how the loop works just (i<=num/i) condn. ****/******* package javalearning; // This code tries to check the whether no is prime or not by divinding it with 2 3 4 and so on .. until the the divisor is less then or equal to the dividend/divisor
Java
import java.util.Scanner;
 public class forDemo {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input=new Scanner(System.in);
    int num=input.nextInt();
    boolean isPrime=false;
    // this code is to illustrates prime no test using for loop
    for (int i=2; i<=num;i++)
    {
        if (num%i==0 ) {isPrime=false; break;}
        else isPrime=true;
    }
    System.out.println("IS YOUR NO A PRIME : "+isPrime);
}
} /************/
************** Plese forgive me for using long line of codes. Thanks for help

Add Comment

What I have tried:

i tried to use condn as i<=num but i know that would not works so plz help
Posted
Updated 20-Jun-18 0:55am
v3

At the start, your variable is initialised:
int i=2

Then every iteration, this condition is checked:
i<=num

After each iteration, until the condition is met, i is increased:
i++


A prime number is a number not divisible by any integers except 1 and itself. So to check for a prime number, your condition should be:
i<num


Note that in your loop, you don't have to assign isPrime every time to true. You can simplify it to:
boolean isPrime=true;
// this code is to illustrates prime no test using for loop
for (int i=2; i<num;i++)
{
    if (num%i==0 ) {isPrime=false; break;}
}
 
Share this answer
 
v2
A for loop is pretty simple - it comes in four parts:
Java
for (a; b; c) d
Where:
a initialises the loop and sets up to run. This is executed once, and once only each time you code reaches the for
b is run immediately before the body of the loop code ( d ) is executed, and if it evaluates to true then the body is executed. If it doesn't, then teh loop is exited.
c is run immediately after the body of the loop code ( d ) is executed to set up for the next iteration.
d is the body of the loop, and is teh code that is executred once each time the loop goes round.
So if a was
Java
int i = 0
b was
i < 3
c was
i++
then the loop body would execute three times, once for i = 0; then i=1; then finally for i = 2
And you would get
a Set i to 0
b Is i < 3? Yes - continue with loop
d Execute body
c Add one to i
b Is i < 3? Yes - continue with loop
d Execute body
c Add one to i
b Is i < 3? Yes - continue with loop
d Execute body
c Add one to i
bIs i < 3? No - exit loop.

In your case, c is i < num which is making sure that you prime test stops when the value reaches the number ("i <= num" won't help, because by definition, all numbers are divisible by themselves)

As to why you might use i <= num / i Think about it with an example.
Assume num is 41, and your test sequence is:
i    num / i
2         20
3         13
4         10
5         8
6         6
7         5
Once you have got to 6 x 6, there is no point in further testing: because by definition you have already tested for divisibility by 5, 4, 3, and 2!
 
Share this answer
 
Quote:
i cannot understand why we need to us this condn statment (i<=num/i).

This is the end condition of the loop.
To see what is going on, add this code as first line in loop
Java
System.out.println("Divisor : "+i);
System.out.println("End Condition : "+(num/i));

and run the program with a prime numbers or a square and see which divisors are tested.
Other solution is to watch your program as it execute with the debugger.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900