# Thread: Whats wrong with my code

1. Member
Join Date
Jan 2011
Posts
17
Rep Power
0

## Whats wrong with my code

Hello
I am trying to understand how to print prime number, only the number user inputs not all the number from 0 or 1 to whatever input but only the input itself

here is the code
thank you
and this is not my homework, I am just trying to find out what i am doing wrong.
please do make lots of changes to the code, I probably won't understand. I am not looking for solution, and just want to know what i am doing wrong.
PHP Code:
```import java.util.Scanner;
public class prime1 {
public static void main (String args[]){
Scanner out = new Scanner(System.in);
int userput = out.nextInt();
int IsPrime;
for(int i=2;i<=userput;i++) {
IsPrime = 1;
if(userput % i == 0) {
IsPrime = 0;
//System.out.print("is not prime");
}
if(IsPrime==1) {
System.out.println(i + " is Prime");
}
}
}
}```

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
A simple point, but an important one: use standard Java naming conventions. Classes (and other type names like interfaces) start with a capital letter and variables with a lower case one. So, Prime1 and isPrime.

The code you posted is not actually checking for primeness.

Java Code:
```for(int i = 2; i <= userput; i++) {          // check each number from 2 to the user limit
isPrime = 1;
if(userput % i == 0) {                   // if it is a *factor* of the user limit...
isPrime = 0;                         // ... set isPrime to zero
}
if(isPrime == 1) {                       // otherwise...
System.out.println(i + " is prime"); // ... print an "is prime" message
}
}```

As I hope is clear from my comments added to your code you are really printing an "is prime" message for the numbers which are not factors of the user supplied limit. Not being a factor of some number the user supplies has nothing to do with being prime.

--------------------

Can you say how you would generate - by hand with pencil and paper - the prime numbers between 2 and 100? That's the place to start: with precise (nothing assumed) and comprehensive (nothing missing) steps. Once you have such an algorithim you can begin to write code.

3. The biggest problem I see with your code is that it prints every number that does not divide evenly into the input, for input of 10, my output was:
Java Code:
```Please Enter a Integer: 10
3 is Prime
4 is Prime
6 is Prime
7 is Prime
8 is Prime
9 is Prime```
The reason for this is that you have the statement that prints i is prime in the wrong spot.

It may be easier to write a helper method which returns a boolean, and works like your code. If input % i == 0, return false, if it finishes the loop and never returns, return true.

You could also declare the flag of whether a number is prime or not outside the loop, then depending on what the flag is after the loop print whether input is prime or not.

4. Member
Join Date
Jan 2011
Posts
17
Rep Power
0
Thanx alot guy for your help I am understand it better, since I renamed my program from prime1 to Prime and Isprime to isZrime, I am just a newbie, I don't know if Prime and isPrime will conflict or not.

I made some changes can you tell me what I am doing wrong here.

PHP Code:
```import java.util.Scanner;
public class Prime {
public static void main (String args[]){
Scanner out = new Scanner(System.in);
int userput = out.nextInt();
int isZrime=1;
int i;
for(i=2;i<=userput;i++) {
if(userput % i == 0) isZrime = 0;
}

if(isZrime == 1)
System.out.println(userput + " is Prime");
else
System.out.println(userput + " is not Prime");
}
}```

I am getting is not prime output all number except 1
I don't know what i am doing wrong

5. Member
Join Date
Dec 2010
Posts
2
Rep Power
0
Hi,

In your for loop you have included the specified number which is "userput",
so for last execution of your for loop which is "userput" you will get "userput%userput" which is zero so your isZrime is set to zero and so your number shows as non prime.

The solution to this is to remove the "=" from your for loop condition and you for loop should be as follows.

for(i=2;i<userput;i++) {

all the best

6. Member
Join Date
Jan 2011
Posts
17
Rep Power
0
Thanx alot ghaeshr, I am understand now, can i ask you one thing is it ok to put
Java Code:
`i=2;i<=userput-1;i++`
does mean samething as
Java Code:
`i=2;i<userput;i++`
I also want to go over the program and ask if I interpreted it right.

I was always confused why isZrime=1; has to be outside the for loop,
I was dumb.
I was trying to compare two different program and the first one had 2 for loops it was calculating from 0 to userput and 2nd one the one I am doing only the userput itself,

so first one goes like this

PHP Code:
```for(i=2;i<=100;i++)
isZrime =1;
[B]for(i=2;i<userput;i++) {
if(userput % i == 0) isZrime = 0; [/B]
}```
in above code i<=100 is '=' is the reason I had i<=userput
so the bold part in the code box, is the main which checks if userput is factor of i, correct me if I am wrong.

and for my program Scanner is basically acting like the for loop which send the userput value to 2nd for loop for it to be tested and see if it is factor of i.

PHP Code:
```        Scanner out = new Scanner(System.in);
int userput = out.nextInt();
int isZrime=1;
int i;
for(i=2;i<userput;i++) {
if(userput % i == 0) isZrime = 0;
}

if(isZrime == 1)
System.out.println(userput + " is Prime");
else

}
}```

my question is for calculating prime numbers from 0 to userput, which is first loop, lets say user input 100, so the i in send for loop what will it do,
will it test all numbers from 0 to 100 and what will it %(mod) it by just i=2; or something else also.
Thanx again for help sorry for making it long.
Last edited by zit1343; 01-22-2011 at 05:47 PM.

7. One thing you can do to make your program slightly more efficient is to loop from 2 to the square root of the number you are checking for primeness. All numbers that are not prime will be found by the square root of the number.
Java Code:
```double x = Math.sqrt((double)userput);
for(int i = 2; i <= x; i++)```
The following to conditions:
Java Code:
```i < userput;
i <= userput - 1;```
are indeed the same, think about it, if the number is 20, the last number that's on the way to userput is 19, 19 is still less than 20, but the next step would be 20, which is not less than 20, it is equal. If you do <= userput - 1, you are asking if i is less than or equal to 1 less then the input, 20 - 1 is 19, you do want to check 19 so when its 19 i is equal to 19 and it works.

The simplest to print all prime numbers from 1 to x is to create a helper method which defines isPrime, then make a loop from 1 to x
Java Code:
`for(int i = 1; i <= x; i++){`
and on the way check with the helper whether a number is prime.

If you don't want to make a helper method, it isn't much more difficult though. You would simple use 2 for loops, the first specifies which number is prime, the second tests the number you are up to and determines if it's prime.

Having something named Prime and isPrime will not cause any issues, they are not the same word.

8. Member
Join Date
Jan 2011
Posts
17
Rep Power
0
Hey Thanx buddy for explanation..
PHP Code:
```
import java.util.Scanner;
public class PrimeAll {
public static void main(String args[]) {
Scanner one = new Scanner(System.in);
int userput = one.nextInt();
int n, j;
double x = Math.sqrt((double)userput);

for(n=2;n<=x;n++){
boolean p = true;
for(j=2;j<n;j++) {
if(n % j == 0)
p = false;
}

if (p==true) {
System.out.println(n + " is prime");
}
}

}
}```

I have question for this part, if I use this part I don't need the
>>> if(userput % i == 0) isZrime = 0; <<<
this right
Java Code:
```double x = Math.sqrt((double)userput);
for(int i = 2; i <= x; i++)```

9. I'm a little unsure what exactly you are asking, were you able to get the code to compile correctly? You need both that second loop and the if clause. The second loop is defined to loop through the numbers from 2 until current number - 1 and test if it can be divided, if it can, it is not a prime number, so both numbers are necessary. Also, you may want to declare x in the first loop, how you have it, x will always be the square root of the input, in actuality you want this number to change depending on the current number you are testing. Try leaving x defined there, then adding a
Java Code:
`System.out.println(x);`
in the first loop and see what happens.

You also don't have to define i and j, you can declare and initialize them in the loop:
Java Code:
```for(int n = 1; n < input; n++){
//do something
for(int j = 2; j < x; j++){
}```
make sure to add the print x in the first loop to see what it is doing. You are close but there are some flaws still with your code.

If I am not entirely clear please tell me what is unclear so I can attempt to clarify for you.

here is some pseudo code of what should happen:
Java Code:
```prompt for a number
loop from 1 to the number input
test EACH number for primeness, if its prime, print it.
if it is not prime the first loop continues```
the idea of doing until the square root is for some efficiency on larger numbers, if you are testing 1 million, whats faster, a loop from 2 to 1 mill, or 2 to the square root of 1 mill?

something else:
Java Code:
```   public class Prime {
public static void main(String args[]) {
Scanner one = new Scanner(System.in);
int userput = one.nextInt();
int n, j;
double x = Math.sqrt((double)userput);

for(n=2;n<=x;n++){//loop from 2 - square root of input, why? What is this loops goal, is it to
//determine primeness, or is it to pass a number to a second loop which determines primeness?
boolean p = true;
for(j=2;j<n;j++) {//this loop is almost correct, its goal is to determine primeness,
//however, it is looping incorrectly because of the first loop, j can ONLY reach x because of
//your first loop, and if your first loop is corrected, this will be slow on large numbers
if(n % j == 0)
p = false;
}

if (p==true) {
System.out.println(n + " is prime");
}
}

}```
Last edited by sunde887; 01-23-2011 at 07:44 AM.

10. The reason why looping to the square root is more efficient then looping to the number being tested:

Take a number that you know for a fact is not prime, lets say 20.

now loop down from 20 to 1. what is the first number you will find that divides evenly into 20? It should be 10, now consider this
Java Code:
`10x = 20`
what is x?

Try the same thing with more numbers you know are not prime

Do you notice anything that may make looping to the square root more efficient?

Lets say you want to test 25 million and see if it's prime, if you had to do this by hand would you rather spend a day or two testing the numbers from 1 to 5000, or spending a much longer time testing EVERY number from 1 - 25 million. Also, is it possible to find a number under the square root of 25 million which doesn't have a larger factor?

if you find a number under 5000 that divides evenly into 25 million, it will always have another number greater than 5000 which also divides into 25 million.

Let me know if this clears things up.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•