CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    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.

  2. #2
    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?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    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.

  4. #4
    Join Date
    Oct 2011
    Posts
    5

    Re: College lab help

    Quote Originally Posted by keang View Post
    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!");
       }
       }
     }

  5. #5
    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/

  6. #6
    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    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.

  8. #8
    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<= number 2a++)
          {
             
    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.

  9. #9
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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.
    Norm

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

Featured