Another Question

Another Question

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 Tutorials/Questions & Answers:
Another Question - Java Beginners
Another Question  This is the question I posted yesterday: "I'm... with a command line menu. The choices are p, f, q and n. In case of choice n, ask the user... great! The only thing is how do I make the command line to accept letters p, f, q
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
Advertisements
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
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
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  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
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  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
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:ADS_TO_REPLACE_1 1)form1.jsp: <html> <form method="post" action
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
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
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
ArrayList as an argument to another class
ArrayList as an argument to another class  how do you pass arraylists as arguments to another class, and how does the method/class accept this arraylist for use
frame update another frame.
frame update another frame.   How do I make a link or form in one frame update another frame
Question on pooling
Question on pooling  which server supports pooling?Application server or web server
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
Core Java Interview Question, Interview Question
the classes in that package but not from classes in another package Question: How... in another package, but this cannot be done for friendly features Question: What... Core Java Interview Question Page 27  
basic question
basic question  how we create a new table with the same structure and data of another table
This question in Jmeter
This question in Jmeter  hi..i want to send request(xml) 100 times but each time in request xml one element needs to change, otherwise request will be failed since its duplicate
Question on javascript
Question on javascript  This is not exactly what i wanted....If i enter in 2nd combobox as name of child then it should show child id in 1st combobox automatically and vice versa
C# question
Create a Solution by name Bank with Classes Account.cs, SavingsAccount.cs and savingsTest.cs to Demonstrate Abstract Classes in C#  1st question : Create a Solution by name Bank with Classes Account.cs, SavingsAccount.cs
question for c++
question for c++   write Program to swap between two numbers if first number is greater than second number and total of two numbers greater than 10
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
Inheritance question?
Inheritance question?  Q3)Create a class hierarchy as Company extended by AccountsDepartment. AccountsDepartment extended by AccountsReceivable... as AccountsPayable. please send the code of this question soon....plzzzzzzzz  
Inheritance question?
Inheritance question?  Q3)Create a class hierarchy as Company extended by AccountsDepartment. AccountsDepartment extended by AccountsReceivable... as AccountsPayable. please send the code of this question soon....plzzzzzzzz View
Core Java Interview Question, Interview Question
to another. Question: If an object is garbage collected, can it become reachable... Core Java Interview Question Page 21       Question: How are the elements of a GridLayout
jsp question..
jsp question..  what should do in deployment descriptor after adding jstl jar files.. I am the the following message in console screen; The absolute cannot be resolved in either web.xml or the jar files deployed
jsp question..
jsp question..  what should do in deployment descriptor after adding jstl jar files.. I am the the following message in console screen; The absolute cannot be resolved in either web.xml or the jar files deployed
Question in Java ??
Question in Java ??   Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number squrfoot
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
jsp question
jsp question   sir plz tell me how can I create a page which is similar to feedback form in which if one option is empty then other option is inaccessible. for example it consists of name address etc. if name field is not filled
jsp question
jsp question   sir plz tell me how can I create a page which is similar to feedback form in which if one option is empty then other option is inaccessible. for example it consists of name address etc. if name field is not filled
jsf: generate another panelGrid
jsf: generate another panelGrid  hello the following is my code print("code sample"); <%@ taglib uri="http://java.sun.com/jsf/html" prefix... generate another panelgrid with the same number of columns and rows (Each time I
Core Java Interview Question, Interview Question
Core Java Interview Question Page 12       Question: What is a transient variable? Answer... Question: Which containers use a border Layout as their default layout? Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 16       Question: Which containers may have a MenuBar? Answer: Frame ADS_TO_REPLACE_1 Question: How are commas used
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. ADS_TO_REPLACE_1 Question
Question Java>?
Question Java>?  **ïâ??· Create a new class called Bank and in the main method use class Account (lab 4) to create an array of 10 accounts. ïâ??· Enter data for all accounts (account number,name, balance). ïâ??· Add
Question on java
Question on java  why java is not purely oop?   java is not purely OOP because 1.) Multiple Inheritance is not supported.ADS_TO_REPLACE_1 2.) It uses primitive data type. 3.) static method can be called without
Ajax Question
Ajax Question   I am having 3 fields in jsp they are Fieldname,Fieldtype,Required. If i enter as Fieldname studentid,Fieldtype as checkbox,Required as true(i.e., mandatory field as *).It should store their details in database
java question
java question   Create a class called Complex to perform arithmetic operations with complex numbers. 1- Use double variables to represent the fields of the class. 2- Provide a no-argument constructor with default values in case

Ads