Write a program in Java to print prime factors of a number entered as per user's choice. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a program in Java to print prime factors of a number entered as per user's choice.

https://code.sololearn.com/c4xrUIo0NaL9/?ref=app

2nd Mar 2020, 9:28 AM
Vatsal
4 Answers
+ 8
Here is one solution in C++ language, you try to customize it for yourself.😉 • https://code.sololearn.com/cPj45ZyHsmz8/?ref=app
2nd Mar 2020, 12:08 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 8
Vatsal Agarwal sorry, You want to find the prime factors of a given number. Here is one way to do it but first make a correction, pay attention how you write the main() method! public static void main(String args[]) { Scanner in = new Scanner(System.in); // Enter a number: int n = in.nextInt(); for (int i = 2; i*i <= n; i++) { while (n % i == 0) { System.out.printf("%d ", i); n /= i; } } if (n > 1) { System.out.printf("%d", n); } } Also, check this out: • https://www.sololearn.com/learn/4714/?ref=app
2nd Mar 2020, 7:17 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 7
You should accompany your question with an attempt. This Q&A forum is for specific programming questions. https://www.sololearn.com/Discuss/1316935/?ref=app Please show your code here so that we can help you https://www.sololearn.com/post/75089/?ref=app
2nd Mar 2020, 9:31 AM
Gordon
Gordon - avatar
+ 5
There are plenty of bugs. First of all: Correct the indentations. That will make it easier for you to find out that you closed a curly bracket at the wrong line.... Also: Line 7 println (typo) Line 8 nextInt() it's a method Line 10 int j isn't initialized
2nd Mar 2020, 5:56 PM
Tashi N
Tashi N - avatar