03-09-2013, 07:36 PM
This program will ask you for a number and then it will tell you if your number is prime or not.
[code2=java]import java.util.Scanner;
public class primeFinder {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("What number would you like to find if it is prime or not:");
int userNum = scanner.nextInt();
int i;
for (i = 2; i < userNum;i++)
{
int g = userNum % i;
if (g == 0)
{
System.out.println("Your number is not prime!");
break;
}
}
if(i == userNum)
{
System.out.println("Your number is prime!");
}
}
}[/code2]
[code2=java]import java.util.Scanner;
public class primeFinder {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("What number would you like to find if it is prime or not:");
int userNum = scanner.nextInt();
int i;
for (i = 2; i < userNum;i++)
{
int g = userNum % i;
if (g == 0)
{
System.out.println("Your number is not prime!");
break;
}
}
if(i == userNum)
{
System.out.println("Your number is prime!");
}
}
}[/code2]