# Thread: Program to detect the number 7 and its multiples

1. Member Join Date
Apr 2015
Posts
2
Rep Power
0

## 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.  Reply With Quote

2. ## 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  Reply With Quote

3. Member Join Date
Apr 2015
Posts
2
Rep Power
0

## Re: Program to detect the number 7 and its multiples Originally Posted by JosAH 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```  Reply With Quote

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

## 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  Reply With Quote

5. ## 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  Reply With Quote

cycle, detect, java, loop, numbers 