Results 1 to 10 of 10
Thread: Whats wrong with my code
 01222011, 02:35 AM #1Member
 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); System.out.print("Please Enter a Integer: "); 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"); } } } }
 01222011, 03:24 AM #2Moderator
 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.
 01222011, 03:33 AM #3
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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
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.
 01222011, 03:25 PM #4Member
 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); System.out.print("Please Enter a Integer: "); 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
 01222011, 03:40 PM #5Member
 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
 01222011, 04:44 PM #6Member
 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<=userput1;i++
Java Code:i=2;i<userput;i++
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] }
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); System.out.print("Please Enter a Integer: "); 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; 01222011 at 04:47 PM.
 01222011, 09:47 PM #7
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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++)
Java Code:i < userput; i <= userput  1;
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++){
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.
 01222011, 10:44 PM #8Member
 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); System.out.print("Please Enter A integer: "); 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++)
 01232011, 06:38 AM #9
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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);
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++){ }
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
something else:
this is your code commented
Java Code:public class Prime { public static void main(String args[]) { Scanner one = new Scanner(System.in); System.out.print("Please Enter A integer: "); 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; 01232011 at 06:44 AM.
 01242011, 01:06 AM #10
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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
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.
Similar Threads

whats wrong with this code
By hamzeeco in forum Java AppletsReplies: 1Last Post: 12312010, 02:19 PM 
whats wrong with my code?
By beandip408 in forum New To JavaReplies: 6Last Post: 11032010, 12:01 AM 
Whats wrong with this code???
By javanew in forum New To JavaReplies: 4Last Post: 03282010, 05:46 PM 
Whats wrong with this code?
By bbtgirl in forum New To JavaReplies: 2Last Post: 02252009, 03:51 AM 
Whats wrong with my code???
By Soda in forum New To JavaReplies: 2Last Post: 12062007, 12:54 PM
Bookmarks