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);
}
}
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);
}
}
CAN BE ALSO DONE WITHOUT USING BUFFERED READER,INA SHORT AND A CLEAN WAY!!
ReplyDeleteYes, 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.
ReplyDeletethanxxxx,.... its better and useful
ReplyDeletewhy divide i by 2
Deletedividing by 2 is good approach, because for any numbers its divisor will never be greater than half of the number.
Deletefor 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
its simple
ReplyDeleteclass s
{
void main(int n)
{
for(int i=1;i<=n;i=++)
{
if(s%i==0)
{
System.out.println(i);
}
}
}
}
Nice one dude....
Deletebut its showing error for me variable sis not found
DeleteBy the way why did you divide a by 2
ReplyDeletethanks a lot for nice post
ReplyDeletecan u write a program to calculate from 1 to user defined number (ex:100) and prints which number has highest factors among them.
ReplyDeletemy mail id:pavan.obbeli89@gmail.com
Sir Can it be also done using do while loop
ReplyDeleteplease can you tell me how i can do it using a while loop? pleaseeee
Delete@java programmer sir please can you tell me why did you divided the no by 2
ReplyDeletecan you give a program including the no 1 and excluding the no itself
ReplyDeletecan you give a program including the no 1 and the number itself
ReplyDeletetysm!!! 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!!!
ReplyDeleteJava is horrible
ReplyDeleteThnx a loooooooot...
ReplyDeleteNice................
ReplyDeleteBasic 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/
How to do with loop
ReplyDeleteGood approach to a easiest program
ReplyDeleteEasiest way to do that
ReplyDeleteThanx.....
Simplest program
ReplyDeleteimport 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);
}
}
Useless
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteDetermining 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.
ReplyDeletegörüntülüshow
ReplyDeleteücretli show
U1V5P2