Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Coders Wanted! Prime Numbers nxn table Help!!!?
How can I output a nxn table for my prime numbers program??
I got it to compile and run fine without the table but now the next step is to get the table to print. I tried and experiment so long that now I really need help. DO NOT OFFER ME FAKE OR SPAM SITES. I hate it when users try to get away with it and earn pts. Anyways that's my only rule against answering this question. Thanks to those who offer me the appropriate code help.
See code:
import java.util.*;
public class IsPrime {
public static Scanner scanner = new Scanner(System.in);
.
public static int readInt() {
System.out.print("Enter an integer: ");
while (!scanner.hasNextInt()) {
scanner.next();
System.out.print("That was not an integer. Please
try again: ");
}
return scanner.nextInt();
}
public static boolean isPrime(int n) {
if (n < 2) return false;
if (n % 2 == 0)
return (n == 2);
for (int i=3; i*i<=n; i+=2)
if (n % i == 0)
return true;
}
public static void main(String[] args) {
System.out.println("Prime Numbers\n");
int n = readInt();
if (isPrime(n))
System.out.printf("%d is a prime.\n",n);
else
System.out.printf("%d is not a prime.\n",n);
//THE PART I NEED HELP IN.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.printf("%4d",i%j);
System.out.println();
}
}
}
1 Answer
- Robin CLv 41 decade agoFavorite Answer
your isPrime function is incorrect.
Given the none prime number 18..
18 % 3 = 0.
Just fyi...
I don't understand what you need help with for the nxn table. I'm not sure if i%j is the correct number though.