Home Answers Viewqa Java-Beginners Another Question

 
 


Pam
Another Question
1 Answer(s)      3 years and 7 months ago
Posted in : Java Beginners

View Answers

November 23, 2009 at 12:11 PM


Hi Friend,

Save the following code in the java file with name 'PrimesCMDLine.java'.

import java.util.*;

class PrimesCMDLine {
public static void checkPrime(long num){
long i=0;
for ( i = 2; i < num; i++) {
long n = num % i;
if (n == 0) {
System.out.println("Entered number is not Prime!");
break;
}
}
if (i == num) {
System.out.println("Number is Prime!");
}
}



public static void findFactors(long n){
System.out.print("The prime factorization of " + n + " is: ");
for (long i = 2; i <= n / i; i++) {
while (n % i == 0) {

n = n / i;
System.out.print(i + " ");
}
}
if (n > 1) System.out.println(n);
else System.out.println();
}


public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
String menu = "";
System.out.println("Test");
System.out.println();
System.out.println("p. check prime or not");
System.out.println("f. prime factors");
System.out.println("e. Exit");

boolean quit = false;
do{
System.out.print("Please enter your choice: ");
menu = scan.next();
System.out.println();
char ch=menu.charAt(0);
switch(ch) {
case 'p':
System.out.print("Enter number: ");
long num=scan.nextLong();
PrimesCMDLine .checkPrime(num);
break;
case 'f':
System.out.print("Enter number: ");
long num1=scan.nextLong();
PrimesCMDLine .findFactors(num1);
System.out.println();
break;
case 'e':
quit = true;
break;
default:
System.out.println("Invalid Entry!");
}
}
while (!quit);
}
}

Thanks









Related Pages:
Another Question - Java Beginners
Another Question  This is the question I posted yesterday: "I'm trying to write a program that has a text field for input, two buttons and the output text area. A user is asked to type in a number. The first button when pressed
question
question  i need a simple javascript or jsp code to send message from one mail to another mail
question
question  i need a simple javascript or jsp code to send message from one mail to another mail
question
question  i need a jsp/html program which includes drop down menu for leave types and a button for apply leave.and need another jsp to validate types of leave and then insert in to data base using mysql
question
question  Sir, How to stream video on one computer which is playing on another PC in LAN using java + socket / RMI . if you have any idea about that please help me and give the source code
question
question  good afternoon sir, i need a jsp/html program which includes drop down menu for leave types and a button for apply leave.and need another jsp to insert types of leave which is selected by the user ,in to data base
question
on Leave Another grid to show the employees who are on leave Using JPannel,java
question
question  hi good morning. how to retrive the data from one form of jsp to another form.please help me   Hi Friend, Try the following code: 1)form1.jsp: <html> <form method="post" action="form2.jsp"> <
question
question  Sir, how to write the following code in jsp , avoid swing components , please give me the following code in another method , iwant to avoid swing . /* * To change this template, choose Tools | Templates
Question in Eclipse
Question in Eclipse   ** Hello EveryOne , L Have Question in Eclipse ?!! Question is : A.( The square class) Design a class named Square... variable side. 5.Add another constructor that takes one argument, the side
Question in Eclipse ??
Question in Eclipse ??  ** Hello EveryOne , L Have Question in Eclipse ?!! Question is : A.( The square class) Design a class named Square... of instance variable side. 5.Add another constructor that takes one argument, the side
sir i,have a another assignment for these question plz help me sir - JavaMail
sir i,have a another assignment for these question plz help me sir  ... serialization. Write another program to read that file, convert to Rupee.... Design another thread that generates fibonacci numbers and writes them
Question in eclipes ??!!
Question in eclipes ??!!  Hooole EveryOne I have Question in eclipes !!? Question is : The objectives of this lab are 1.Using the ââ?¬Å? thisÃ... ) Design another class to test the Complex class. Ensure that all your methods
basic question
basic question  how we create a new table with the same structure and data of another table
Question on javascript
Question on javascript  There will be two combo box in HTML form.If selecting in one combo box(ex:child id=1),the corresponding one to one matched value(ex:Child name=ashish) get selected automatically in another combo box.How
java question
java question  comparator and comparable   Differences: a)A comparable object is capable of comparing itself with another object while a comparator object is capable of comparing two different objects. b
java question
java question  i created a form in one jsp page and in another jsp page i retrieved all the details which the user had entered ,in second jsp in a tabular format and added a delete button to every row of dat table now i want
J2ME question
J2ME question  Lets say i have 2 screens. One for new user, another for existing user. Currently, the midlet contains radio boxes that allows users to choose which category are they. How do i call the screen for new user when
java question
java question  what r the Comparator vs Comparable   Differences: a)A comparable object is capable of comparing itself with another object while a comparator object is capable of comparing two different objects. b
jsp question
jsp question  i send the request one jsp page to another jsp than how i access the first jsp variable in the servlet? i m able to access only the second jsp page variable but not first jsp page?   Hi Friend, Follow
get the value from another class - Java Beginners
get the value from another class  Hello to all, I have stupid question. I have main.java and ConfigXML.java(read my config xml file). Code from ConfigXML.java [code] NodeList flnamelist = flcon.getElementsByTagName
Core Java Interview Question, Interview Question
Core Java Interview Question Page 21       Question: How are the elements of a GridLayout... and are laid out using the squares of a grid. Question: What an I/O filter? Answer: An I
How to call the run method from another class?
How to call the run method from another class?  Sorry, if this is a stupid question. I would like to find out how to call the run method that is located in java.awt.EventQueue.invokeLater(new Runnable() { public void
Core Java Interview Question, Interview Question
in that package but not from classes in another package Question: How can you access... Core Java Interview Question Page 27       Modifiers Question: What are access modifiers
Core Java Interview Question, Interview Question
Core Java Interview Question Page 12       Question: What is a transient variable? Answer: A transient variable is a variable that may not be serialized. Question: Which
Core Java Interview Question, Interview Question
another thread invokes the object's notify() or notifyAll() methods. Question... Core Java Interview Question Page 16       Question: Which containers may have a MenuBar
Core Java Interview Question, Interview Question
Core Java Interview Question Page 2       Question: Does it matter... the IOException. Exception's subclasses have to be caught first. Question: Can an inner
Core Java Interview Question, Interview Question
Core Java Interview Question Page 24       Question: Why do we need public static void main... not have a public constructor? Question: What is the difference between
Core Java Interview Question, Interview Question
Core Java Interview Question Page 9       Question: What are the uses of Serialization... the servers in a cluster. Question: what is a collection ?  Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 28       Conversion Casting and Promotion Question: What... primitive types to be accessed as objects. Question: What are the four general
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
java project question
java project question  how to calculate bill extracting quantity n price from one database table and inserting the result into another in java   Hi Friend, Visit Here Thanks
Question
Question   When there is an exception in my program how java runtime system handles
question
question  dear sir/madam my question is how to compare two text format in java..we are java beginners..so we need the complete source code for above mentioned question...we have to compare each and every word
Java basic question...
Java basic question...   Design a Java Application project for maintaining Employee Information in a company. Follow the instructions below... employees. (vii) Create another class named Employee and declare the main
question
question  Dear sir i had some typing mistake at previous question so its my humble request to let me know the steps to start the tomcat6 under the tomcat directory
Core Java Interview Question, Interview Question
until another thread calls notify or notifyAll. Question: What... Core Java Interview Question Page 6       Question: How can i tell what state a thread
JAVASCRIPT insert Question -
? I understand that one may use the YES/NO to decide whether to goto another
Java insertion sort question
Java insertion sort question  I've got another program that I need help with. I am trying to write a Java method that accepts an array of strings, and sorts the strings using the insertion sort algorithm. Then I need to write
C++ question 3
C++ question - add the two matrices and display the elements of resultant matrix.   Write a program to accept from user elements of two 2x3 matrices...]); printf("\n"); } getch(); }   If you another example
Core Java Interview Question, Interview Question
Core Java Interview Question Page 10       Question: What is the difference between notify... by a call to notify (so it is often better to use notifyAll than notify). Question
java project question
java project question  how to calculate bill extracting quantity n price from one database table and inserting the result into another in java   Hi Friend, Try the following code: import java.sql.*; import java.util.
question
question  Gud morning sir, I have asked u some question regarding jsp in saturaday for that i didnot find any answere in which u send me the some of the links.U have asked me the specify some details. There is a entity name
Question?
Question?  My question is how to: Add a menu bar to the program with a File menu. In the File menu, add a submenu (JMenuItem) called About. When the user clicks on the About menu item, display a JOptionPane message dialog
calling one jsp from another jsp - JSP-Servlet
calling one jsp from another jsp  Hi All, In my web application I have two jsp files(one.jsp and two.jsp). I have written a seperate method....   one.jsp two.jsp   Hi, The above question
XML Interviews Question page19
XML Interviews Question page19   ... namespaces? I don't know the answer to this question, but the likely reason... document to another document. An early draft of the XML namespaces recommendation
Please , can any one answer this Question?
Please , can any one answer this Question?  How to cummunicate one web application to another web application in different places