Java prime numbers detector
Write a program that uses two nested for loops and the modulus operator (%) to detect and print prime numbers. A prime numbers are integral numbers that are not evenly divisible by any other numbers except themselves and 1. Use your program to print all the prime numbers from 0 to 10,000. Print 10 numbers per output line, separated by two spaces.- asked by Raj on our Facebook profile
package com.itcuties.questions;
public class Answer {
public static void main(String[] args) {
int found=0;
for (int i=1; i<=10000; i++) {
for (int j=1; j <= i; j++) {
if (i % j == 0 && i != j && j != 1) { // watch out for the j==1
break;
}
if (i == j)
if ((++found)%10 == 0)
System.out.println(i);
else
System.out.print(i+" ");
}
}
}
}

Leave a Reply
Want to join the discussion?Feel free to contribute!