Results 1 to 2 of 2
  1. #1
    UmairBaloch is offline Member
    Join Date
    Nov 2012
    Rep Power

    Post 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.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)
    		if (check>1)
    		System.out.print("The number is Composite");
    		System.out.print("The number is Prime"); 
    Java Code:
    I've tried (num<strlmt && num>endlmt);
    System.out.println("the number is not valid");
    but I dont know how to put this in loop

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Bali, Indonesia
    Rep Power

    Default 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.

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. Replies: 4
    Last Post: 02-13-2011, 12:03 PM
  4. Copy Default table model to another default table model?
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 04-28-2010, 04:08 PM
  5. Replies: 1
    Last Post: 03-10-2009, 01:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts