Solve both problems with java code Factoring of integers. Write a program that a
ID: 3916193 • Letter: S
Question
Solve both problems with java code
Factoring of integers. Write a program that asks the user for an integer and then print prints out all its factors. For example, when the user enters 150, the program should Use a class FactorGenerator with a constructor FactorGenerator(int numberToFactor) and methods nextFactor and hasMoreFactors. Supply a class FactorPrinter whose main method reads a user input, constructs a FactorCenerator object, and prints the factors Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print ?? 13 17 19 number if it is not divisible by any number except 1 Recall that a number is a prime and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class Prime- Printer whose main method reads a user input, constructs a PrimeGenerator object, and prints the primes.Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
public class FactorGenerator {
//constructor
FactorGenerator(int numberToFactor)
{
int i=2,n=numberToFactor;
while(hasMoreFactors(n,i))
{
i = nextFactor(n,i);
System.out.println(i);
n=n/i;
}
}
int nextFactor(int n,int i)
{
while(n%i!=0)i++;
return i;//returing next factor
}
boolean hasMoreFactors(int n,int i)
{
if(n/i != 0)
return true;//has factors
return false;//no factors left
}
}
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
public class FactorPrinter {
//main method
public static void main(String argv[])
{
int n;
Scanner sc = new Scanner(System.in);
//asking input..
System.out.print("Enter number to print factors:");
n = sc.nextInt();//reading input
//creating factorgenerator object
//to print factors
FactorGenerator f = new FactorGenerator(n);
}
}
output:
run:
Enter number to print factors:150
2
3
5
5
BUILD SUCCESSFUL (total time: 7 seconds)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
public class PrimeGenerator {
//constructor
PrimeGenerator(int n)
{
int i=1;
while(true)
{
i = nextPrime(i,n);
if(i>n)break;
System.out.println(i);
}
}
//method to generate next prime
int nextPrime(int i,int n)
{
i++;
if(isPrime(i))
return i;
return nextPrime(i,n);
}
//method that checks whether n is prime or not
boolean isPrime(int n)
{
int i=2;
int c=0;
while(i<n)//finding prime or not
{
if(n%i==0)
{
c=1;
break;
}
i++;
}
if(c==0)return true;
return false;
}
}
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Vamsi
*/
public class primePrinter {
//main method
public static void main(String argv[])
{
int n;
Scanner s = new Scanner(System.in);
//reading input
System.out.print("Enter number:");
n =s.nextInt();
PrimeGenerator p = new PrimeGenerator(n);
}
}
run:
Enter number:20
2
3
5
7
11
13
17
19
BUILD SUCCESSFUL (total time: 2 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.