Check Prime Number using a for loop
Java
xxxxxxxxxx
public class Main {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
29 is a prime number.
Check Prime Number using a while loop
Java
xxxxxxxxxx
public class Main {
public static void main(String[] args) {
int num = 33, i = 2;
boolean flag = false;
while (i <= num / 2) {
if (num % i == 0) {
flag = true;
break;
}
++i;
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
33 is not a prime number.
Linux - How to Install anc-api-tools
Linux - How to Install curvedns
Linux - How to Install zenmap
Python - Check if a Number is Positive, Negative or 0
C - Count Number of Digits in an Integer
C++ - Check Whether a Number is Prime or Not
C - Find Largest Number Using Dynamic Memory Allocation
C++ - Check Leap Year
JavaScript - Check Whether a String Starts and Ends With Certain Characters
Kotlin - Reverse a Number
Linux - How to Install zstd
Linux - How to Install xbitmaps
Kotlin - Round a Number to n Decimal Places
C++ - Find Quotient and Remainder
Java - Reverse a Number
Python - Writing Excel (XLSX) Files
Kotlin - Find Factorial of a Number
Java - Create Pyramid and Pattern
We have been online since 2021 and 1 millions of people around the globe have visited our website since then
More visitors every month