Results 1 to 2 of 2
Thread: Default value in while loop
- 12-16-2012, 02:17 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
Default value in while loop
I'm supposed to make a program that reads an integer between two given limits and determines if it's prime or composite. I've made this program using while loop and it runs successfully, what I want to do is that if I enter an integer outside the limits, it must show a message "The number is not valid"
code is
Java Code:import java.util.Scanner; class Pri{ public static void main(String a[]){ Scanner scan = new Scanner(System.in); System.out.print("Enter the Starting limit: "); int strlmt = scan.nextInt(); System.out.print("Enter the Ending limit: "); int endlmt = scan.nextInt(); System.out.print("Enter any number between given limit: "); int num=scan.nextInt(); int count = 2, check = 0; while (count<=num){ if (num%count==0) check++; count++; } if (check>1) System.out.print("The number is Composite"); else System.out.print("The number is Prime"); } }but I dont know how to put this in loopJava Code:I've tried (num<strlmt && num>endlmt); System.out.println("the number is not valid");
Thanks.
- 12-16-2012, 05:49 PM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Default value in while loop
Use OR instead AND in this case. You can't have a number smaller then the start limit and bigger then the end limit at the same time.
Website: Learn Java by Examples
Similar Threads
-
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
program automatically chooses the default in switch inside loop
By jethph in forum New To JavaReplies: 4Last Post: 02-13-2011, 12:03 PM -
Copy Default table model to another default table model?
By greatmajestics in forum AWT / SwingReplies: 2Last Post: 04-28-2010, 04:08 PM -
[SOLVED] Access to default session deniedAccess to default session denied
By jazz2k8 in forum NetworkingReplies: 1Last Post: 03-10-2009, 01:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks