Question: JAVA Fix: Smallest divisor > 1 Debug and fix one of these three questions. Read an integer from the keyboard. If 1 or smaller, print
JAVA
Fix: Smallest divisor > 1
Debug and fix one of these three questions.
Read an integer from the keyboard. If 1 or smaller, print 1. If larger than 1 print the smallest integer divisor of the number. For instance for 2 print 2, for 17 print 17 (it is prime), for 27 print 3.
import java.util.Scanner;
public class SmallestDivisor1 { // Method to compute the smallest divisor. (Lab 6b) public static int smallestDivisor(int div) { int i; int num; for (i = 2; i <= div / 2; i++) { if (div % i == 0) { break; } } return 2; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num = scan.nextInt(); System.out.println(smallestDivisor(num)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
