- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
k-th prime factor of a given number in java
Following is the Java program which prints the kth prime factor of a number n, when k and n are given.
Program
import java.util.Scanner; public class KthPrimeFactor { public static void main(String args[]) { int number, k, factor = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter a number :"); number = sc.nextInt(); System.out.println("Enter the k value :"); k = sc.nextInt(); int temp = k-1; for(int i = 2; i< number; i++) { while(number%i == 0) { if(temp == 0) { factor = i; } temp--; System.out.println(i+" "); number = number/i; } } System.out.println(k+"th prime factor of the given number is :"+factor); if(number >2) { System.out.println(number); } } }
Output
Enter a number : 216 Enter the k value : 3 2 2 2 3 3 3 3th prime factor of the given number is :2
Advertisements