- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Program To Find Prime Numbers
Here's a Java program to find prime numbers up to a specified limit:
public class PrimeNumbers {
public static void main(String[] args) {
int limit = 100; // Specify the limit
System.out.println("Prime numbers up to " + limit + ":");
// Loop through numbers from 2 to the limit
for (int i = 2; i <= limit; i++) {
boolean isPrime = true;
// Check if the number is prime
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
// Print the prime number
if (isPrime) {
System.out.print(i + " ");
}
}
}
}
This program iterates through numbers from 2 to the specified limit. For each number, it checks if it is divisible by any number from 2 to its square root. If it is not divisible by any of these numbers, then it is considered a prime number and printed.