Timer in the code

Timer in the code

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









Related Tutorials/Questions & Answers:
ModuleNotFoundError: No module named 'code-timer'
ModuleNotFoundError: No module named 'code-timer'  Hi, My Python... 'code-timer' How to remove the ModuleNotFoundError: No module named 'code... have to install padas library. You can install code-timer python
ModuleNotFoundError: No module named 'code-timer'
ModuleNotFoundError: No module named 'code-timer'  Hi, My Python... 'code-timer' How to remove the ModuleNotFoundError: No module named 'code... have to install padas library. You can install code-timer python
Advertisements
ModuleNotFoundError: No module named 'code-timer'
ModuleNotFoundError: No module named 'code-timer'  Hi, My Python... 'code-timer' How to remove the ModuleNotFoundError: No module named 'code... have to install padas library. You can install code-timer python
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
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
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
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
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
ModuleNotFoundError: No module named 'timer'
ModuleNotFoundError: No module named 'timer'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'timer' How to remove the ModuleNotFoundError: No module named 'timer'
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
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
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
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
ModuleNotFoundError: No module named 'djangocms-timer'
ModuleNotFoundError: No module named 'djangocms-timer'  Hi, My... named 'djangocms-timer' How to remove the ModuleNotFoundError: No module named 'djangocms-timer' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-timer'
ModuleNotFoundError: No module named 'django-timer'  Hi, My Python... 'django-timer' How to remove the ModuleNotFoundError: No module named 'django-timer' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-timer'
ModuleNotFoundError: No module named 'django-timer'  Hi, My Python... 'django-timer' How to remove the ModuleNotFoundError: No module named 'django-timer' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'easy-timer'
ModuleNotFoundError: No module named 'easy-timer'  Hi, My Python... 'easy-timer' How to remove the ModuleNotFoundError: No module named 'easy-timer' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'easy-timer'
ModuleNotFoundError: No module named 'easy-timer'  Hi, My Python... 'easy-timer' How to remove the ModuleNotFoundError: No module named 'easy-timer' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'gevent_timer'
ModuleNotFoundError: No module named 'gevent_timer'  Hi, My Python... 'gevent_timer' How to remove the ModuleNotFoundError: No module named 'gevent_timer' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'magic-timer'
ModuleNotFoundError: No module named 'magic-timer'  Hi, My Python... 'magic-timer' How to remove the ModuleNotFoundError: No module named 'magic-timer' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'maple_timer'
ModuleNotFoundError: No module named 'maple_timer'  Hi, My Python... 'maple_timer' How to remove the ModuleNotFoundError: No module named 'maple_timer' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'monocle_timer'
ModuleNotFoundError: No module named 'monocle_timer'  Hi, My... 'monocle_timer' How to remove the ModuleNotFoundError: No module named 'monocle_timer' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'noop-timer'
ModuleNotFoundError: No module named 'noop-timer'  Hi, My Python... 'noop-timer' How to remove the ModuleNotFoundError: No module named 'noop-timer' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'nose-timer'
ModuleNotFoundError: No module named 'nose-timer'  Hi, My Python... 'nose-timer' How to remove the ModuleNotFoundError: No module named 'nose-timer' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'osimis-timer'
ModuleNotFoundError: No module named 'osimis-timer'  Hi, My Python... 'osimis-timer' How to remove the ModuleNotFoundError: No module named 'osimis-timer' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'perf-timer'
ModuleNotFoundError: No module named 'perf-timer'  Hi, My Python... 'perf-timer' How to remove the ModuleNotFoundError: No module named 'perf-timer' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'pomodoro-timer'
ModuleNotFoundError: No module named 'pomodoro-timer'  Hi, My... named 'pomodoro-timer' How to remove the ModuleNotFoundError: No module named 'pomodoro-timer' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'pref_timer'
ModuleNotFoundError: No module named 'pref_timer'  Hi, My Python... 'pref_timer' How to remove the ModuleNotFoundError: No module named 'pref_timer' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'process-timer'
ModuleNotFoundError: No module named 'process-timer'  Hi, My... 'process-timer' How to remove the ModuleNotFoundError: No module named 'process-timer' error? Thanks   Hi, In your python

Ads