Results 1 to 5 of 5
  1. #1
    Programmar is offline Member
    Join Date
    Apr 2015
    Posts
    2
    Rep Power
    0

    Lightbulb Program to detect the number 7 and its multiples

    Hello, I just started learning Java, I was asked to write a simple program that prints a message if it encounters the number 7 or its multiple.
    This should be achievable using simple loops and simple operations...

    Here is my attempt:
    Java Code:
    Scanner scan = new Scanner(System.in);
    int i, j, temp;
    		
    		i = scan.nextInt(); // Awaiting user input
    		j = scan.nextInt(); // Awaiting user input
    		
    		if (i > j){       // i = 10 j = 0
    			temp = i;    // temp = 10
    			i = j;      // i = 0       j = 0       temp = 10
    			j = temp;    // i = 0   j = 10
    			System.out.println();
    		}else
    			System.out.println();
    		
    		for ( ; i<=j; i ++)
    			if (  ((i%10) == 7 ) ||  ((i%7) == 0)  ){ // if the number contains a 7 or is its multiple
    				System.out.println("!!!");
    			} else {
    				System.out.println(i);
    			}
    Now it works fine for numbers that are less than 70...
    Example for output with i = 15, and j = 25:
    Java Code:
    15
    16
    !!!
    18
    19
    20
    !!!
    22
    23
    24
    25
    I am not sure what to do in case if for example 'i' and 'j' are very big numbers
    I mean, I need it to detect the 7s even if it in the thousands place, actually - no matter how big is the number...
    So far I only made it to work for numbers that are less than 70...

    Thanks in advance
    Last edited by Programmar; 04-25-2015 at 03:54 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Program to detect the number 7 and its multiples

    There are several methods to skin this cat; I would do it like this: for a number 'n', the expression ""+n is the String representation of that number n; Strings have handy methods, such as contains( ...); I would use it like this:

    Java Code:
    String rep= ""+n; // String representation of 'n'
    boolean has7= rep.contains("7");
    If there is at least one 7 in the number, variable has7 is true.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Programmar is offline Member
    Join Date
    Apr 2015
    Posts
    2
    Rep Power
    0

    Default Re: Program to detect the number 7 and its multiples

    Quote Originally Posted by JosAH View Post
    Jos

    Hi I tried using your method:

    Java Code:
    public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		int i, j, temp;
    		String rep;
    		boolean has7;
    
    		i = scan.nextInt(); // Awaiting user input
    		j = scan.nextInt(); // Awaiting user input
    
    		if (i > j){       // i = 10 j = 0
    			temp = i;    // temp = 10
    			i = j;      // i = 0       j = 0       temp = 10
    			j = temp;    // i = 0   j = 10
    			System.out.println();
    		}else
    			System.out.println();
    
    
    		for ( ; i<=j; i ++){
    			for (rep = ""+i, has7 = rep.contains("7"); (has7 == true); has7 = false){
    				System.out.println("!!!");
    
    			}
    			for (rep = ""+i, has7 = rep.contains("7"); has7 == false; has7 = true){
    				System.out.println(i);
    			}
    
    
    		}
    	}
    with i=63 and j=73 the output was:

    Java Code:
    63
    64
    65
    66
    !!!
    68
    69
    !!!
    !!!
    !!!
    !!!
    Now I tried making it print a message if the number was divisible by 7 but it ended up being an infinite loop and nothing happened...
    Java Code:
    			for (rep = ""+i, has7 = rep.contains("7"); ((has7 == true) || (i%7 == 0)); has7 = false){
    				System.out.println("!!!");



    Now, another way I tried is this way:

    Java Code:
    public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		int i, j, temp, k;
    
    		i = scan.nextInt(); // Awaiting user input
    		j = scan.nextInt(); // Awaiting user input
    
    		if (i > j){       // i = 10 j = 0
    			temp = i;    // temp = 10
    			i = j;      // i = 0       j = 0       temp = 10
    			j = temp;    // i = 0   j = 10
    			System.out.println();
    		}else
    			System.out.println();
    
    
    		for ( ; i<=j; i ++) {
    			for (k = i; k >= 1; k /= 10){
    				if ( (k % 7 == 0) || (k % 10 == 7) ){
    					System.out.println("!!!");
    				}
    			}
    			System.out.println(i);	
    		}
    	}


    It worked but I don't know how to get the program not to print the ones that have a 7 or are divisible by it...
    i=63 and j=73:

    Java Code:
    !!!
    63
    64
    65
    66
    !!!
    67
    68
    69
    !!!
    !!!
    70
    !!!
    71
    !!!
    72
    !!!
    73

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Program to detect the number 7 and its multiples

    There are several ways. One is to use the continue keyword in conjunction with statement labels. If you are not familiar with them check out this

    Another way is to set a flag and use a break statement. Then use the flag later to evaluate whether to print or not.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Program to detect the number 7 and its multiples

    I'd create two simple methods, say, 'div7(int n)' and 'has7(int n)' that return true if a n number n can be divided by 7 or has a 7 in its decimal representation respectively; those two methods keep my main( ... ) method simpe:

    Java Code:
    for (; i <= j; i++)
       if (div7(i) || has7(i))
           // number i fits the description
    You do already know how to make those two little methods ... don't be afraid of methods; they untangle convoluted programming logic and chop up the problem in to manageable parts.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. How Can I Detect When My Program Is Ending
    By petec in forum New To Java
    Replies: 16
    Last Post: 02-28-2014, 03:59 PM
  2. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  3. How to have program detect own name
    By DarkHarbinger in forum Advanced Java
    Replies: 4
    Last Post: 08-14-2012, 08:20 AM
  4. Print Odd Multiples of 3
    By mp3_1 in forum New To Java
    Replies: 9
    Last Post: 11-22-2011, 09:09 PM

Tags for this Thread

Posting Permissions

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