Another Question

Ads

 
 

Share on Google+Share on Google+

Pam
Another Question
1 Answer(s)      8 years ago
Posted in : Java Beginners
Ads
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

Ads









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  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 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  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: 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
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
frame update another frame.
frame update another frame.   How do I make a link or form in one frame update another frame
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
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
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
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
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
I/O to another applications
I/O to another applications  **What if there exists an application that needs a certain input and will give an output. Using Java, how can we invoke that application with an input and read the resulted output?**   Hi
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

Ads

 
Advertisement null

Ads