Follow all instructions below: Keep the program\'s code as simple as possible -
ID: 3797367 • Letter: F
Question
Follow all instructions below:
Keep the program's code as simple as possible - begineer level with no advanced level Java concepts/skills
Write a program that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1)The code should ask the user if they want to run the program again. You will need: 1. A scanner object to read 1. The user's numerical entry 2. The user's string response 2. Variables to store: 1. The user's entry (should be an integer) 2. The user's response (should be a string) 3. The number of primes (this will help to determine how many numbers are printed before moving to the next line) (Include additional variables as needed) 3. Several loops 1. A do...while loop to repeat the program. This loop will contain: 1. A while loop for input validation 2. A while loop to determine if the number is prime (this loop may contain a nested for loop to check primes from 2 up to the user's entry) An if statement to determine when to skip a line after 10 numbers has been printed 4. Lots of comments for documentationExplanation / Answer
Here is my solution in java :-
package com;
import java.util.*;
public class Prime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number=0; // number to take input from user
String response; // input to take response from user
Scanner sc; // declaring scanner object
int linc=0; //declaring int var to take care of line
sc = new Scanner(System.in);
boolean flag; // declaring boolean variable to know user wants to continue run program or not
do{
System.out.print("do you want to continue(Y/N): ");
// sc.next();
response = sc.next(); // asking user to give response
if(response.equals("Y")||response.equals("y"))
{
System.out.print("Enter the number: ");
while(!sc.hasNextInt()) //verifying if input is an integer or not
{
System.out.println("invalid input try again");
sc.next();
continue;
}
number=sc.nextInt();
String primeNumbers=""; //declaring string which stores prime number
int i=2;
if(number>=2){
while(i <= number) // while loop to determine prime number from 2 to number
{
int counter=0;
for(int num =i; num>=1; num--) //prime number logic
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
if(linc<10){
primeNumbers = primeNumbers + i + " ";
linc++;
}
else
{
linc=1;
primeNumbers+= " "+i+" "; //if linc value cross 10 then added to break the line and print prime numbers in new line
}
}
i++;
}
System.out.println("Prime numbers from 2 to num are :");
System.out.println(primeNumbers);
}
else{
System.out.println("Number should be greater then or equal to 2");
}
}
else
response= "N";
flag = response.equals("N");
}while(!flag);
sc.close();
}
}