| Q & A Home |
| Math |
| Science |
| History |
| IT & Web |
| Programming |
| Health |
| Business |
| Arts & Humanities |
| Social Studies |
| Engineering & Technology |
| Arts & Entertainment |
| Humanities |
| Sports |
| Auto |
| Hobbies |
| Books and Literature |
| Electronics |
| Food & Drink |
| Jobs & Education |
| Law & Government |
| Travel & Places |
| People & Society |
| Beauty & Health |
| Animals & Plants |
| Other |
Obaidullah Ahrar Oasif 29 Jun, 2024 11:29:21 PM 1 150
Best Answer: Use if statements to check for each condition separately:
If the number is 0 or 1, it cannot be prime.
If the number is 2, it is prime number.
If the number is indivisible by other numbers, it is prime.
public static boolean isPrime(int n) {
if (n == 0 || n == 1) {
return false;
}
if (n == 2) {
return true;
}
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
40. How do you sum all the elements in an array?
Use for loop to iterate through the array and keep adding the elements in that array.
int[] array = { 1, 2, 3, 4, 5 };
int sum = 0;
for (int i : array)
sum += i;
System.out.println(sum);
As you get prepared for your job interview, we hope that these Coding Interview Questions have provided more insight into what types of questions you are likely to come across.
Obaidullah Ahrar Oasif 29 Jun, 2024 11:29:21 PM