
Clarissa M.
asked 11/14/20HELP IN JAVA CODE ASAP
Write a JAVA CODE that:
- Display all prime numbers less than 50
- Display all factors of 50
- Display only the prime factors of 50
- Determine whether 50 is a perfect square or
- Exit the program
Has to run in loop to get user input until the user opts to exit.
1 Expert Answer

Patrick B. answered 11/14/20
Math and computer tutor/teacher
import java.io.*;
class Clarissa
{
private long N;
//returns the first divisor of N , or ZER0 if prime
public long IsPrime( long n)
{
long longIntNumReturn=0;
for (long iLoop=2; iLoop<n/2+1; iLoop++)
{
if ((n%iLoop)==0)
{
longIntNumReturn=iLoop;
break;
}
}
return(longIntNumReturn);
}
public long GetN() { return(N); }
protected int Menu()
{
Console console = System.console();
String inbuff;
this.N=-1;
while (this.N<2)
{
System.out.println("*************************************************");
System.out.print(" Please input the Integer greater than 2 :>");
inbuff = console.readLine();
this.N = Long.parseLong(inbuff);
}
int iMenuReturn=-1;
while ((iMenuReturn<0) || (iMenuReturn>5))
{
System.out.println("***************************************************");
System.out.println("******** MENU *********");
System.out.println("***************************************************");
System.out.println(" (1) All Prime Numbers Less than "+ this.N );
System.out.println(" (2) ALL FACTORS of " + this.N);
System.out.println(" (3) ONLY the Prime Factors of "+ this.N);
System.out.println(" (4) is " + this.N + " a Perfect Square ??? ");
System.out.println(" (5) Writes " +this.N + " as a Product of Primes ");
System.out.println("***************************************************");
System.out.print(" PLEASE INPUT YOUR SELECTION or ZER0 to EXIT//QUIT :>");
inbuff = console.readLine();
iMenuReturn = Integer.parseInt(inbuff);
}
return(iMenuReturn);
}
public void Go()
{
int iMenuReturn=-1;
while (
(iMenuReturn = Menu()) != 0
)
{
switch (iMenuReturn)
{
case 1:
{
for (long iLoop=2; iLoop<this.N; iLoop++)
{
if (IsPrime(iLoop)==0)
{
//prime # less than N has been found
System.out.println(iLoop + " is a prime number less than " + this.N);
}
} //for loop
break;
} //case1
case 2:
{
for (long iLoop=1; iLoop<this.N; iLoop++)
{
if ((this.N % iLoop)==0)
{
//factor has been found
System.out.println(iLoop + " is a factor of " + this.N);
}
} //for loop
System.out.println(this.N + " is a factor of itself "); //number is a factor of itself
break;
} //case 2
case 3:
{
for (long iLoop=2; iLoop<this.N; iLoop++)
{
if ((this.N % iLoop)==0)
{
//factor has been found....
if (IsPrime(iLoop)==0)
{
//...AND it's PRIME!!!
System.out.println(iLoop + " is a PRIME factor of " + this.N);
} //prime factor
} //factor
} //for loop
break;
} //case 2
case 4:
{
boolean perfect_square_flag=false;
long nSqr=-1;
long squareRoot=-1;
for (long iLoop=2; iLoop<this.N; iLoop++)
{
nSqr = iLoop*iLoop;
if (nSqr== this.N)
{
perfect_square_flag = true;
squareRoot=iLoop;
break;
}
if (nSqr>this.N)
{
break;
}
}//for loop
if (perfect_square_flag)
{
System.out.println(this.N + " is a perfect square : the square root is " + squareRoot );
}
else
{
System.out.println( this.N + " is NOT a perfect square: the square root is " + Math.sqrt(this.N) );
}
break;
} //case 4
case 5:
{
long longIntNum = this.N ;
long primeFactor=-1;
System.out.println(this.N + " is the product of the following prime factors:");
while (
( primeFactor = IsPrime(longIntNum))!=0
)
{
System.out.println(primeFactor);
longIntNum = longIntNum / primeFactor; //divides the factor out
} //while
System.out.println(longIntNum); //outputs the last prime factor that stopped the loop
break;
} //case 5
} //switch
} //while
System.out.println("Bye!");
} //Go
public static void main(String args[])
{
Clarissa GoClarissa = new Clarissa();
GoClarissa.Go();
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
I uploaded this source code for you in the RESOURCES section under this link. You must RENAME the file from Clarissa.txt to Clarissa.java;11/14/20