write a program in java to print prime factorisation
Answers
Answered by
1
Prime factors of a number are those factors which are prime in nature and by which the number itself is completely divisible (1 will not be taken as prime number).
Few such numbers are:
Prime Factors of 24 are 2, 2, 2, 3
Prime Factors of 6 are 2, 3
Solution:
/**
* The class PrimeFactors inputs a number and prints all its PrimeFactors
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.io.*;
class PrimeFactors
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("Enter a Number : ");
n=Integer.parseInt(br.readLine());
System.out.print("The Prime Factors of "+n+" are : ");
int i=2;
while(n>1)
{
if(n%i == 0)
{
System.out.print(i+" ");
n=n/i;
}
else
i++;
}
}
}
Output:
Enter a Number : 378
The Prime Factors of 378 are: 2 3 3 3 7
Few such numbers are:
Prime Factors of 24 are 2, 2, 2, 3
Prime Factors of 6 are 2, 3
Solution:
/**
* The class PrimeFactors inputs a number and prints all its PrimeFactors
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.io.*;
class PrimeFactors
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("Enter a Number : ");
n=Integer.parseInt(br.readLine());
System.out.print("The Prime Factors of "+n+" are : ");
int i=2;
while(n>1)
{
if(n%i == 0)
{
System.out.print(i+" ");
n=n/i;
}
else
i++;
}
}
}
Output:
Enter a Number : 378
The Prime Factors of 378 are: 2 3 3 3 7
Jeetkrl:
I knew it. I was just checking how brainly works. Thank you for your support
Similar questions
Social Sciences,
7 months ago
Hindi,
7 months ago
Math,
7 months ago
Math,
1 year ago
CBSE BOARD X,
1 year ago