Java project: I have a solution that deals with 7 digits number. but I want you
ID: 3821541 • Letter: J
Question
Java project:
I have a solution that deals with 7 digits number. but I want you to calculate the full 10 digit number such as 123 456 7890
My Solution:
import java.util.*;
public class Prime
{
public static void main(String[] args)
{
int num,i;
Scanner in = new Scanner(System.in);
System.out.print("Enter the phone number as an integer (<=0 to exit): ");
num = in.nextInt();
while(num>0)
{
if(isPrime(num))
System.out.println(num+" is a prime number");
else
{
System.out.println("The Prime Factors of "+num+" are:");
i=2;
while(i<=num )
{
if(num%i==0)
{
System.out.print( i+" ");
num=num/i;
}
else
i++;
}
System.out.println();
System.out.print("Enter the phone number as an integer (<=0 to exit): ");
num=in.nextInt();
}
}
}
public static boolean isPrime(int n)
{
int i;
for(i=2;i<Math.sqrt(n);i++)
if(n%i==0) //if you find a factor the number isn't prime
return false;
return true; // get here only if no factors
}
}
Please Do. Thanks!
Project 9 Thursday April 20 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 that 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. We will discuss possible algorithms and determine the most efficient one to be used by everyone. After it runs as a console program, using the GUI example from my website as a guide, convert this program to a graphics program. To debug your compiled program use System.out.printlno to follow intermediate values of your variables to see where your code does not follow the algorithm.Explanation / Answer
HI, Instead of int you can use long data types.
I have modified code.
import java.util.*;
public class Prime
{
public static void main(String[] args)
{
int i;
long num;
Scanner in = new Scanner(System.in);
System.out.print("Enter the phone number as an integer (<=0 to exit): ");
num = in.nextLong();
while(num>0)
{
if(isPrime(num))
System.out.println(num+" is a prime number");
else
{
System.out.println("The Prime Factors of "+num+" are:");
i=2;
while(i<=num )
{
if(num%i==0)
{
System.out.print( i+" ");
num=num/i;
}
else
i++;
}
System.out.println();
System.out.print("Enter the phone number as an integer (<=0 to exit): ");
num=in.nextInt();
}
}
}
public static boolean isPrime(long n)
{
int i;
for(i=2;i<Math.sqrt(n);i++)
if(n%i==0) //if you find a factor the number isn't prime
return false;
return true; // get here only if no factors
}
}
/*
Sample run:
Enter the phone number as an integer (<=0 to exit): 1234567890
The Prime Factors of 1234567890 are:
2 3 3 5 3607 3803
Enter the phone number as an integer (<=0 to exit): 0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.