Go to Top

Friday 4 November 2011

To print all the factors of a number

Question 67 : Write a program in Java to print all the factors of a number.

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);
    }
}

1 comment:

  1. can u pls write it using import java.util.*;
    pls i need it

    ReplyDelete

ShareThis