Ads

this project is to determine if a phone number is a prime number. if the number is a prime number then print a message to the effect. if the number is not a prime number then print the prime factors of the number. allow the user to continue entering numbers for as long as he or she wishes.

i was given the following, but its not quite right and perhaps not quite complete:

for(count = 5; count <3; count = count +2)

{

x=0;

sq = Math.sqrt(count)

prime = true;

while(a[x]<sq,&&prime)

{

if(count % a(x) ==0)

prime = false;

else

x++;

}

if(prime)

a[index] = count;

index++;

}

i was given the following, but its not quite right and perhaps not quite complete:

for(count = 5; count <3; count = count +2)

{

x=0;

sq = Math.sqrt(count)

prime = true;

while(a[x]<sq,&&prime)

{

if(count % a(x) ==0)

prime = false;

else

x++;

}

if(prime)

a[index] = count;

index++;

}

Ads

View Answers

May 10, 2010 at 12:57 PM

Hi Friend,

Try the following code:

import java.util.*;

class CheckPrime{

static boolean isPrime(long number){

boolean isPrime=false;

int i=(int) Math.ceil(Math.sqrt(number));

while (i>1){

if ((number!=i) && (number%i==0)){

isPrime=false;

break;

}

else if (!isPrime)

isPrime=true;

--i;

}

return isPrime;

}

public static void main(String[] args){

Scanner input=new Scanner(System.in);

int menu = 0;

boolean quit = false;

System.out.println("1. Check");

System.out.println("2. Exit");

do{

System.out.print("Please enter your choice: ");

menu = input.nextInt();

System.out.println();

switch(menu) {

case 1:

System.out.println("Enter number to check: ");

long num=input.nextLong();

if(isPrime(num)){

System.out.println("Hello");

}

else{

System.out.println("Factors are:");

for(int i=1;i<num;i++){

if(num % i == 0){

if(isPrime(i)){

System.out.println(i);

}

}

}

}

break;

case 2:

quit = true;

break;

}

}

while(!quit);

}

}

Thanks

Try the following code:

import java.util.*;

class CheckPrime{

static boolean isPrime(long number){

boolean isPrime=false;

int i=(int) Math.ceil(Math.sqrt(number));

while (i>1){

if ((number!=i) && (number%i==0)){

isPrime=false;

break;

}

else if (!isPrime)

isPrime=true;

--i;

}

return isPrime;

}

public static void main(String[] args){

Scanner input=new Scanner(System.in);

int menu = 0;

boolean quit = false;

System.out.println("1. Check");

System.out.println("2. Exit");

do{

System.out.print("Please enter your choice: ");

menu = input.nextInt();

System.out.println();

switch(menu) {

case 1:

System.out.println("Enter number to check: ");

long num=input.nextLong();

if(isPrime(num)){

System.out.println("Hello");

}

else{

System.out.println("Factors are:");

for(int i=1;i<num;i++){

if(num % i == 0){

if(isPrime(i)){

System.out.println(i);

}

}

}

}

break;

case 2:

quit = true;

break;

}

}

while(!quit);

}

}

Thanks

Ads

Related Tutorials/Questions & Answers: