1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

im a beginner and my teacher gives this question 2 solve which i definetly need help.

Discussion in 'Java' started by ranchani, Jun 25, 2010.

  1. ranchani

    ranchani New Member

    Joined:
    Jun 25, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Write a JAVA program in which a positive whole number is first read in. A ‘for-loop’ is used to extract the factors of the given number. If there are no factors in the range 2 to (n-1), then that number should be a prime number. A break statement is used to transfer the control out of the for loop, if there is a factor:undecided ?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    Show your code i.e. the attempt to code it
     
  3. ranchani

    ranchani New Member

    Joined:
    Jun 25, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    actually i don't know to code it............tats my probs....i just know how 2 begin

    Code:
       import java.io.*;
                class Number
                {
                Public static void main (String [] args) throws IOException
                          {
                          System.out.print(“Enter the number:”);
                         InputStreamReader reader = new InputStreamReader(System.in);
                         BufferedReader in =new BufferedReader(reader);
                         String text=in.readLine();
     
    Last edited by a moderator: Jun 28, 2010
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    Then first get the code as submission of codes from any online forum will not be of any help
     
  5. ranchani

    ranchani New Member

    Joined:
    Jun 25, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    thanz 4 the advice ill try 2 write the code as much as i can......................:pleased:
     
  6. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    maybe this is what you want.Try and tell me.

    Code:
    import java.io.*;
    public class Number1{
        
    public static void main (String [] args) throws IOException{
        
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader in =new BufferedReader(reader);
        String text="";
        int number=-1;
        while(number<=0){
            System.out.print("Enter the number:");
            text=in.readLine();
            try{
               number=Integer.parseInt(text);
               if (number<=0) System.out.println("number must be positive and not 0!!!");
            }catch(NumberFormatException e){
               System.out.println("error enter a number!!!");
            }
            
        }
        //now you have a number to check for factors
        if (number==1) 
               System.out.println("1 is prime number");
        else{
            int n=number;
            int i;
            int factors=0;
            for (i=2;i<n;i++){
                while (number%i==0){
                     factors++;
                     number/=i;
                     System.out.println("factor="+i);
                }
                
            }
            if (factors>0) 
                 System.out.println("\nnot a prime number");
            else
                 System.out.println("\nit is a prime number!!!");
        }    
    }
    }
    
    
     
  7. Chantheeya

    Chantheeya New Member

    Joined:
    Mar 7, 2010
    Messages:
    6
    Likes Received:
    1
    Trophy Points:
    0
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    Hey varsha!! :) I hope u got the code for this program... Otherwise, get it from me in class!!! ;)
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    Sounds interesting.
     
  9. ranchani

    ranchani New Member

    Joined:
    Jun 25, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    hey thankz every1 4 helping me................
     
  10. atsukoarai86

    atsukoarai86 New Member

    Joined:
    Aug 5, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    systems analyst / db developer
    Location:
    Jupiter
    Re: im a beginner and my teacher gives this question 2 solve which i definetly need h

    I think your problem may be more of a failure of logic than a failure of java. If I were you, I would attempt to formulate an algorithm to solve the problem in question then flesh it out in pseudocode then convert it to java syntax.

    If the problem is that you don't understand how to actually write the code in java but you understand how to solve the problem, that would be a question that I'm sure your java teacher would be happy to answer.
     

Share This Page