Go to Top

Thursday 11 August 2011

To find out the factors of a number

Question 41 : Write a program in Java to find the factors of a number given by the user.


Java Program :



import java.io.*;
class factors
{
 static void main()throws IOException
 {
     BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
     int a,i;
     System.out.print("Enter the number : ");
     a=Integer.parseInt(buf.readLine());
     System.out.print("\n");
     System.out.print("The factors are : ");
     for(i=1;i<=a/2;i++)
     {
         if(a%i==0)
         System.out.print(i+",");
        }
        System.out.print(a);
    }
}

28 comments:

  1. CAN BE ALSO DONE WITHOUT USING BUFFERED READER,INA SHORT AND A CLEAN WAY!!

    ReplyDelete
  2. Yes, your point is true, but buffered reader improves the ease of using the program. I would have easily passed the number as parameter, but then would a normal user know how to invoke a method and enter the arguments ? This was just a method of doing the program. However, the main part below the buffered reader part remains the same.

    ReplyDelete
  3. thanxxxx,.... its better and useful

    ReplyDelete
    Replies
    1. why divide i by 2

      Delete
    2. dividing by 2 is good approach, because for any numbers its divisor will never be greater than half of the number.
      for eg: if need to find divisor for 6, we try to divide it by 4(greater than 6/2 = 3), it is common sense that it will never be a divisor for 6. this approach will reduce unnecessary loops

      Delete
  4. its simple
    class s
    {
    void main(int n)
    {
    for(int i=1;i<=n;i=++)
    {
    if(s%i==0)
    {
    System.out.println(i);
    }
    }
    }
    }

    ReplyDelete
    Replies
    1. Nice one dude....

      Delete
    2. but its showing error for me variable sis not found

      Delete
  5. By the way why did you divide a by 2

    ReplyDelete
  6. can u write a program to calculate from 1 to user defined number (ex:100) and prints which number has highest factors among them.
    my mail id:pavan.obbeli89@gmail.com

    ReplyDelete
  7. Sir Can it be also done using do while loop

    ReplyDelete
    Replies
    1. please can you tell me how i can do it using a while loop? pleaseeee

      Delete
  8. @java programmer sir please can you tell me why did you divided the no by 2

    ReplyDelete
  9. can you give a program including the no 1 and excluding the no itself

    ReplyDelete
  10. can you give a program including the no 1 and the number itself

    ReplyDelete
  11. tysm!!! that was really very helpful...i had my exams nd there i referred to yur program nd all my doubts were cleared!!! KEEP POSTING SUCH PROGRAMS!!! itz very useful fr beginners lyk me!!!

    ReplyDelete
  12. Thnx a loooooooot...

    ReplyDelete
  13. Nice................
    Basic C Program for Computer Programmer who want to learn. If you have simple wish you may visit. To get all update stay with us.
    Outline:
    • Language: C and Simscript.
    • if (Condition) statement and Nested if statement is used to determine the given number is positive, negative, even or odd. Another way we solve this program using if-else-if ladder.
    • When the given condition or expression is true the statement associated with it executed.
    To know details:

    http://www.secufoon.com/write-a-c-program-for-determining-a-number-is-positive-negative-even-or-odd/

    ReplyDelete
  14. How to do with loop

    ReplyDelete
  15. Good approach to a easiest program

    ReplyDelete
  16. Simplest program
    import java.io.*;
    public class Factors
    {
    public static void main(String args[])throws IOException
    {
    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
    int a,i;

    System.out.print("Enter the number : ");
    a=Integer.parseInt(buf.readLine());
    System.out.print("\n");

    System.out.print("The factors are : ");
    for(i=1;i<=a/2;i++)
    {
    if(a%i==0)
    System.out.print(i+",");
    }

    System.out.print(a);
    }
    }

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Determining factors of a number involves dissecting it into its divisors, unveiling its mathematical building blocks. Device Streaming Fix This process aids in prime factorization, simplifying fractions, and solving equations.

    ReplyDelete

ShareThis