Results 1 to 11 of 11
Thread: Problem on project
 05292013, 12:00 AM #1Member
 Join Date
 May 2013
 Posts
 12
 Rep Power
 0
Problem on project
For a class I'm taking on java (basic level college course) I'm having some trouble on the homework.
We are writing a program that evaluates whether or not a number is prime or not. I have this semi working, but for the numbers 2,9, and 93 I cannot get the program to read the correct answer.
I was wondering if anyone could point out whatever I'm clearly missing here because I'm stumped.
here is the code:
import java.util.Scanner;
public class Assignment3 {
public static void main (String[]args){
Scanner in=new Scanner (System.in);
int num;
System.out.print("Please enter a positive integer:");
num=in.nextInt();
for (int i=2; i<num;i++){
//for (i=num1;i>1;i){
if (num%i==0){
//composite
System.out.print(num+" Composite ");
System.out.print(num/i);
break;
}
else if (num%i>0){
//prime
System.out.print(num+ " is prime");
break;
}
}
}
}
 05292013, 12:13 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,931
 Rep Power
 4
Re: Problem on project
So you are using modularity to check to see if any of the divisors from 2 to the number1 evenly divide the number. The problem is the algorithm. Only one value from 2 to inum1 needs to divide the number to determine if it is composite. But all the numbers from 2 to inum1 need to return a nonzero value to determine primality.
Also, your loop for 2 will not run because it starts at 2 and fails the conditional when the input value is 2.
Regards,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 05292013, 12:17 AM #3Member
 Join Date
 May 2013
 Posts
 12
 Rep Power
 0
Re: Problem on project
What does that translate to in actual code? I'm a total novice (5th day of class) and what your saying only makes vague sense if anything to me...
It's much easier for me to understand things in terms of reverse engineering (if that isn't too much hassle for you).
 05292013, 12:38 AM #4Senior Member
 Join Date
 Nov 2012
 Posts
 258
 Rep Power
 2
Re: Problem on project
what you need to do is:
1, get the number from a user or hard code it:(call this n)
2, start at 2 and see if the number can be divided by 2 (n%2 ==0) if it cant so (n%2 !=0) then try the next number:
2b, continue step to increasing the division (call this i) untill you reach the original number, if all these i values return a remainder then the number is prime:
Java Code:int n = in.nextInt(); for(int i=2;i<n;i++) { //check every number between 2 and your number if(n%i==0) System.out.println("NOT PRIME, " + n + "Divides by: " + i ); } else { System.out.println("" + n + " is prime"); }
 05292013, 12:38 AM #5Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,931
 Rep Power
 4
Re: Problem on project
Something I forgot to mention is to place you code in code tages [CODE][/CODE] so you code if formatted. I have done it with your supplied code.
Java Code:import java.util.Scanner; public class Assignment3 { public static void main (String[]args){ Scanner in=new Scanner (System.in); int num; System.out.print("Please enter a positive integer:"); num=in.nextInt(); for (int i=2; i<num;i++){ //for (i=num1;i>1;i){ if (num%i==0){ //composite System.out.print(num+" Composite "); System.out.print(num/i); break; } else if (num%i>0){ //prime System.out.print(num+ " is prime"); break; } } } }
Also, it is customary to *not* write code to solve problems for posters in this forum. Most will help you diagnose the problem and may offer code snippets or complete apps to demonstrate a very small part of the overall submitted code.
Regards,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 05292013, 12:41 AM #6Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,931
 Rep Power
 4
Re: Problem on project
I believe you have syntax problems. Did you try this yourself??
Regards,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 05292013, 01:01 AM #7Member
 Join Date
 May 2013
 Posts
 12
 Rep Power
 0
Re: Problem on project
I'm totally clueless how I'm supposed to write this loop and flag. I'm still only vaguely understanding what you are saying. I totally understand the whole "dont write out the whole code for them" part though.
I need more of an explanation of how that happens and how I change it to meet that.
 05292013, 01:19 AM #8Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,931
 Rep Power
 4
Re: Problem on project
Okay, here is some pseudo code.
Java Code:int prime = 0; For div = 2 to numb1 if (div divides numb) { prime = 1; break; } }
Regards,
JimLast edited by jim829; 05292013 at 01:24 AM.
The Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 05292013, 01:26 AM #9Member
 Join Date
 May 2013
 Posts
 12
 Rep Power
 0
Re: Problem on project
I guess a better question might be to ask how to properly flag a number as prime now. I broke down my code more and got the composites to read out correctly, but I still haven't figured out how to read out the primes correctly.
Java Code:import java.util.Scanner; public class Assignment3 { public static void main (String[]args){ Scanner in=new Scanner (System.in); int num,i; System.out.print("Please enter a positive integer:"); num=in.nextInt(); for(i=num1;i>1;i){ if(num%i==0){ System.out.print(num+" Composite "); System.out.print(i); break; } } } }
 05292013, 01:48 AM #10Member
 Join Date
 May 2013
 Posts
 12
 Rep Power
 0
Re: Problem on project
I think I ended up solving the problem. I ended up with this code
Java Code:import java.util.Scanner; public class Assignment3 { public static void main (String[]args){ Scanner in=new Scanner (System.in); int num,i; System.out.print("Please enter a positive integer:"); num=in.nextInt(); if(num==2){ System.out.print("prime"); } for(i=num1;i>1;i){ if(num%i==0){ System.out.print(num+" Composite "); System.out.print(i); return; } } for (i=num1;i>1;i){ if(num%i==1){ System.out.print("prime"); } } } }
 05292013, 02:37 AM #11Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,931
 Rep Power
 4
Re: Problem on project
Well, you're almost there. Your previous effort was the better one. Simply iterate thru the loop, checking to see if the number is composite. As soon as it is, set a flag. Then when you exit the loop, use the flag to determine whether the number is composite or not. Try and go thru this to understand the changes I made.
Java Code:Scanner in=new Scanner (System.in); int num,i; int isPrime = 0; // 0 means prime, 1 means composite System.out.print("Please enter a positive integer:"); num=in.nextInt(); for(i=2; i < num; i++){ // back the way it was if(num%i==0){ isPrime = 1; // System.out.print(num+" Composite "); // System.out.print(i); break; } } // The idea is that if you go thru the loop without finding a divisor, then the number must be prime, so isPrime remains 0. if (isPrime == 0) { // the number is prime } else { // the number is composite }
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
Similar Threads

Problem with rounding up in a school project
By OlivierSillegue in forum New To JavaReplies: 10Last Post: 02132012, 03:10 AM 
java project problem
By mr_byte31 in forum New To JavaReplies: 2Last Post: 11132010, 08:45 PM 
project problem
By arsenal4ever_11 in forum NetBeansReplies: 4Last Post: 05292010, 10:47 AM 
Project: CarProject problem
By Rhez in forum New To JavaReplies: 6Last Post: 10152009, 04:51 AM 
my project problem??help please
By sheckoo in forum New To JavaReplies: 4Last Post: 12062008, 01:14 AM
Bookmarks