Timer in the code

Ads

 
 

Share on Google+Share on Google+

Pam
Timer in the code
1 Answer(s)      7 years ago
Posted in : Java Beginners
Ads
View Answers

November 23, 2009 at 4:34 PM


Hi Friend,

Try the following:

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':
long start = System.currentTimeMillis();
System.out.println("Start: " + start);
System.out.print("Enter number: ");
long num1=scan.nextLong();
PrimesCMDLine.findFactors(num1);
long end = System.currentTimeMillis();
System.out.println("End : " + end);
long elapsedTime = end - start;
System.out.println(elapsedTime + " milli seconds");
System.out.println();
break;
case 'e':
quit = true;
break;
default:
System.out.println("Invalid Entry!");
}
}
while (!quit);
}
}

If you got any error or any problem, then send your code.

Thanks

Ads









Related Tutorials/Questions & Answers:
Timer in the code - Java Beginners
Timer in the code  This is the question I posted yesterday: "I'm.... I'm confused about where and what i should put into the overall code. Also I can't quite figure out how to write the code for the GUI. Could anyone please help
code for timer in java netbeans for online test series countdown timer
code for timer in java netbeans for online test series countdown timer  code for timer in java netbeans for online test series countdown timer
Advertisements
j2me timer and timer task
j2me timer and timer task  have anyone idea to change a direction of bubble, according to changing time.   Please go through the following links: http://www.roseindia.net/j2me/timer-midlet.shtml http
Java timer
Java timer  Hi. I want to use a timer in one of my java projects... how to store the input in a string. Please show me how. Here's the code: import java.util.Timer; import java.util.TimerTask; public class ToDo { Timer
ServletContextListener with Timer
In this section, you will learn how to use timer with ServletContextListener to run code snippet after every 1 minute immediate after the application run
Timer and TimerTask
Timer and TimerTask  i have problem of java.lang.IllegalStateException: Task already scheduled or cancelled
timer using javascript
timer using javascript  How to display timer on HTML page using JavaScript
Difference between Timer and Thread?
Difference between Timer and Thread?  Can anyone tell me about the difference between Timer and Thread, Why we need to have Timer in case we have Thread implimentation startegy in Java
javascript timer countdown
javascript timer countdown  How to disable/enable the javascript timer countdown?   <!-- CSS Code --> #txt { border:none; font... you to start and stop the timer after few seconds. Run the code to see how
Java Timer Task
Java Timer Task  How to assign a task in java at specified date & time (with seconds) using threads
Retrieval data from database against timer
Retrieval data from database against timer  Please send me a sample timer code which retrieve data from the database against timer in JSP
JavaScript Clock Timer
JavaScript Clock Timer...; In this section, we are going to show you a clock timer using JavaScript... than 9, then it will add 0 in front of min, sec or hour value like 09 in the timer
Countdown timer to show a link
Countdown timer to show a link  Hi to everyone! I have this code.... The day arrives, and the link that leads to the songs appears. This is the code (you have to click on the countdown so the page with the code appears at the left
Insertion Sort Timer
Insertion Sort Timer  Welcome all I wanna program in java find the timer of insertion sort and find time complexity for random value thanks all
Maven Dependency geronimo-timer >> 1.0
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0 in your project
Maven Dependency geronimo-timer >> 1.0-M2
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0-M2 in your project
Maven Dependency geronimo-timer >> 1.0-M3
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0-M3 in your project
Maven Dependency geronimo-timer >> 1.0-M4
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0-M4 in your project
Maven Dependency geronimo-timer >> 1.0-M5
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.0-M5 in your project
Maven Dependency geronimo-timer >> 1.1
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.1 in your project
Maven Dependency geronimo-timer >> 1.1.1
You should include the dependency code given in this page to add Maven Dependency of geronimo >> geronimo-timer version1.1.1 in your project
Maven Dependency knife-timer >> 0.0.2
You should include the dependency code given in this page to add Maven Dependency of com.ineunet >> knife-timer version0.0.2 in your project
Maven Dependency knife-timer >> 0.0.3
You should include the dependency code given in this page to add Maven Dependency of com.ineunet >> knife-timer version0.0.3 in your project
Maven Dependency timemanager-timer >> 1.0
You should include the dependency code given in this page to add Maven Dependency of com.rhcloud >> timemanager-timer version1.0 in your project
Maven Dependency timemanager-timer >> 1.6-RELEASE
You should include the dependency code given in this page to add Maven Dependency of com.rhcloud >> timemanager-timer version1.6-RELEASE in your project
Timer - Swing AWT
Timer  How can I make a simple countdown timer in my GUI?  ... { ProgressMonitor progress; static int counter = 0; Timer timer...("Progress bar timer Demo"); setSize(250, 100); setDefaultCloseOperation
Java + Timer concept - Java Beginners
interval) and retrieve values and print it in console. I want code using timer...Java + Timer concept  sir, I am using swing based model. My form... will change after some span of time) and print it in console. I want code and idea
Version of geronimo>geronimo-timer dependency
List of Version of geronimo>geronimo-timer dependency
Version of com.ineunet>knife-timer dependency
List of Version of com.ineunet>knife-timer dependency
Version of com.rhcloud>timemanager-timer dependency
List of Version of com.rhcloud>timemanager-timer dependency
J2ME Timer MIDlet Example
J2ME Timer MIDlet Example       This Example shows how to use of timer class. In this example we are using the Timer class to create the time of execution  of application