In this code im able to print given n prime numbers but im not able to print the last prime number as per the question. I dont know what logic here i should use . please help me in this code

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tn = sc.nextInt();

int n = 1;

for (int i = 1; i <= tn; i++) {
    boolean flag = true;
    for (int div = 2; div <= n - 1; div++) {
        if (n % div == 0) {
            flag = false;

        }

    }
    if (n < 2) {
        flag = false;
    }

    if (flag == true) {

        System.out.println(n);

    }

    n++;

}

}
}

If I ask you a question like print the largest prime number less than 100. how will you do it?
in this part of your code
if (flag == true) {

    System.out.println(n);

}

instead of printing n , store it in a variable, say latest_prime = n; and when n>100 break the entire loop.
you need to follow the same thing in this problem.
here, you also missed to take tn inputs. tn in your code is no of test cases, you need to read tn more inputs.
Thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.