 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: College lab help

1. Junior Member Join Date
Oct 2011
Posts
1

## College lab help

Hi all. I am new and in need of some help. I'm sure it is something easy but I have been staring at it too long. Basically I need to write code that will display the prime factors of an inputted number. I am able to find the factors of the number pretty easily but I am getting bogged down testing if the factors are prime and then printing them. Here is what I have so far:

import java.util.Scanner;

public class lab63 {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

System.out.println ("Enter a number to factor: ");

int number = input.nextInt();
int x = 0;

System.out.print("Factors: ");

while (x<number){
int divisor = 1+x;

if (number%divisor==0){

System.out.print (" "+divisor);

}
x++;
}

System.out.println ();

}

This will find and display the factors but I need to add a loop it so it will find which of these factors are prime and display then. Any help would be really appreciated. Thanks.  Reply With Quote

2. Elite Member Power Poster    Join Date
May 2006
Location
UK
Posts
4,473

## Re: College lab help

Do you have an algorithm for testing if a number is a prime number?  Reply With Quote

3. Junior Member Join Date
Oct 2011
Posts
5

## Re: College lab help

Do a loop within the loop. Basically just do what you did to the numbers to find what is prime and then loop to do the same thing to those numbers.  Reply With Quote

4. Junior Member Join Date
Oct 2011
Posts
5

## Re: College lab help Originally Posted by keang Do you have an algorithm for testing if a number is a prime number?
Code:
```class Prime_number {
public static void main(String[] args) {
int num = 11;
int i;
for (i=2; i < num ;i++ ){
int n = num%i;
if (n==0){
System.out.println("not Prime!");
break;
}
}
if(i == num){
System.out.println("Prime number!");
}
}
}```  Reply With Quote

5. Member Join Date
Aug 2011
Location
West Yorkshire, U.K.
Posts
54

## Re: College lab help

There are more efficient ways of establishing if a number is prime.
You don't need to test if the number divides by 4,6,8... because it will also be divisble by 2.
You don't need to test all the way up to the number itself - the square root of the number is the largest divisor you need to check.

See http://www.mkyong.com/java/how-to-de...umber-in-java/  Reply With Quote

6. Elite Member Power Poster    Join Date
May 2006
Location
UK
Posts
4,473

## Re: College lab help

Ok so now you have your algorithm for finding if the number is prime or not (although I suggest you amend it as AlexVV has suggested) place this code in a method in your lab63 class declared something like:
Code:
`private static boolean isPrime(int num)`
and in your main method call isPrime() whenever you want to test if a number is prime.  Reply With Quote

7. Junior Member Join Date
Aug 2011
Posts
10

## Re: College lab help

You don't really need to determine if the numbers are prime.

I would suggest, however, starting from 2, which is the first prime number.

Also remember that a divisor might be used multiple times.

Example: The prime factors of 12 are 2, 2, and 3. IE: 2 * 2 * 3 = 12.

When the remainder is zero, you might want to (1) print the divisor, (2) divide number by the divisor and store the new smaller value back into number, and (3) try that same divisor again. You might want to increment the divisor only when the remainder is non-zero.  Reply With Quote

8. Junior Member Join Date
Oct 2017
Posts
5

## Re: College lab help

Below is the code,

PHP Code:
``` import java.util.*; public class JavaPrimeNumber {    public static void main(String[] args)    {       int digit;       boolean checkPrime = true;       Scanner sc = new Scanner(System.in);       System.out.println("Enter any number find to prime number in java : ");        int number = sc.nextInt();         for(int a = 2; a <= number / 2; a++)       {          digit = number % a;          if(digit == 0)          {             checkPrime = false;             break;          }       }         if(checkPrime)          System.out.println(number + " is a prime number.");       else          System.out.println(number + " is not a prime number.");       sc.close();    } }  ```
For more you can refer this resource for more on java programs.
Last edited by sidsomashak; April 12th, 2018 at 09:21 AM. Reason: Have indented the code.  Reply With Quote

9. Elite Member  Join Date
Jun 1999
Location
Eastern Florida
Posts
3,850

## Re: College lab help

@sidsomashak This is a 7 year old thread.

You should wrap all posted code in code tags to preserve formatting and make the code easier to read.  Reply With Quote

10. ## Re: College lab help

The for loop can also be improved. Apart from 2, all prime numbers are odd. Also, the bound for checking for divisors should be sqrt, not half (calculate before the loop). So an improvement is to have a special check for 2 as a divisor first. If not, then the start of the loop should be 3 and the increment should be +2 up until sqrt of number.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• Click Here to Expand Forum to Full Width