Tuesday, March 6, 2012

Interactive program for Factor

import java.io.*;
public class Factor {

    public static void main(String [] args)
    {
        getFactor();
        while(true)
        {
        System.out.print("Do you want another number?(YES/NO)  ");
        BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
        try {
            String decision = reader.readLine();
            if(decision.equalsIgnoreCase("YES")|| decision.equalsIgnoreCase("Y"))
            {
                getFactor();
            }
            else
            {
                System.out.println("Exiting...");
                break;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
    public static void getFactor()
    {
    BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
    try {
        System.out.print("Please enter a number for factor:  ");
        int n=Integer.parseInt(reader.readLine());
       
        for(int i=2;i<=n;i++)
        {

      if(i>n/2)
{
i=n;
}

            while (n%i==0)
            {
                System.out.println("  "+i);
                n=n/i;
            }
        }
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    }

No comments:

Post a Comment