Time limit exceeded(PrateekLovesCandy)

Here is my code, My code runs well, but Time limit is exceeded, I don’t know how to reduce the time limit. Can u please help me?

import java.util.Scanner;

public class Main {
public static int solution(int c) {

	int num = 3;
	int val = 1;
	if (c ==1) {
		System.out.println(2);
	}
	else {

	for (int i = 2; i <= c;) {
		for (int j = 2; j <= Math.sqrt(num); j++) {
			if (num % j == 0) {
				val = 0;
				break;
			}
		}
		if (val != 0) {
			i++;
		}
		val = 1;
		num++;
	}
	System.out.println(num-1);
	}
	return 0;
}

public static void main(String[] args) {

	Scanner s = new Scanner(System.in);
	int t = s.nextInt();
	int[] a = new int[t];
	for (int i = 0; i < t; i++) {
		a[i] = s.nextInt();
	}
	for(int i=0;i<t;i++) {
		int b=a[i];
		solution(b);
	}

}

}

Use Sieves of Eratosthenes method to solve this question , it will reduce the time complexity.

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.