java program to find prime numbers from 1 to n
import java.util.Scanner;
public class PrimeNum {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your limit : ");
int limit=sc.nextInt();
for (int i = 1; i < limit; i++) {
boolean k = true;
for (int j = 2; j <= i/2; j++) {
if (i % j == 0) {
k = false;
break;
}
}
if (k) {
System.out.println(i);
}
}
}
}
Java program to check a number weather is it prime or not
import java.util.Scanner;
public class PrimeCheck{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number : ");
int n = sc.nextInt();
int flag = 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
System.out.println("Number is not prime");
flag = 1;
break;
}
}
if (flag == 0)
System.out.println("Number is prime");
}
}
Learned a lot of new things from your post!Good creation ,It's amazing blog
ReplyDeleteCore Java Online Training
Java Online Course
Java Online Training Hyderabad