Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* This is for CS 101 Java class. I can only use \"while\" loops. I cannot use \"

ID: 3724455 • Letter: #

Question

* This is for CS 101 Java class. I can only use "while" loops. I cannot use "for", "do-while" or any other repetition method.*

Your program inputs one positive integer and calculate its prime divisors. Note that prime divisors of a number cannot be greater than this value and cannot be smaller than 2. You may assume the user enters valid inputs, you are not expected to validate the input.

Sample run:

Enter an integer: 60

Prime divisors of 60: 2 3 5

Another sample run:

Enter an integer: 123

Prime divisors of 123: 3 41

This is my code for this assignment but the thing is I couldn't figure out how to print "the prime divisors of this number..." part.

import java.util.Scanner;
  
public class Lab04b
{
  
public static void main(String[] args)
{
  
Scanner in = new Scanner(System.in);
  
//Constants
  
//Variables
int divisor;
int number;
  
//Initialize the variables
System.out.println("Please enter an integer: ");
number = in.nextInt();
divisor = 2;
  
// Program code
while (divisor <= number)
{
while (number % divisor == 0)
{
number = number / divisor;
System.out.print(divisor + " ");
}
divisor = divisor + 1;
}   
  
}
}

Explanation / Answer

import java.util.Scanner;

public class Lab04b

{

  

public static void main (String[] args)

{

Scanner scanner = new Scanner(System.in);

int i =1;

int num =0;

//Empty String

String primeNumbers = "";

System.out.println("Enter the value of n:");

int n = scanner.nextInt();

int divisionValue=n;

scanner.close();

while (i <= n)

{   

int counter=0;

num =i;

while( num>=1)

{

if(i%num==0)

{

counter = counter + 1;

}

num--;

}

if (counter ==2)

{

//Appended the Prime number to the String

if(divisionValue%i==0)

{

primeNumbers = primeNumbers + i + " ";

}

  

}

i++;

}

System.out.println("prime divisors are :");

System.out.println(primeNumbers);

}

}