Find numbers divisible by 3 and 4


Write a program that displays all numbers divisible by 3 and 4 within a range entered by user. Display five numbers per line. Numbers are separated by exactly two spaces.


This is my code



```
        public class Chapter4 {
            public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
                final int NUMBER_OF_PRIMES_PER_LINE = 5;
                int count = 0;
                int[] vars = new int[10];

                for (int i = 0; i < vars.length; i++) {
                    System.out.println("Enter " + (i + 1) + " numbers: ");
                    vars[i] = input.nextInt();

                    if ((vars[i] % 3 == 0) && (vars[i] % 4 == 0)) {
                        count++;
                    }

                    if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
                        System.out.println(vars[i]+"  ");
                    }
                }
            }    
   }
```

I input value 120 10 times, but I only get one 120 displayed at the end.
Your explanation is not consistent. You state that the user is to enter a range as in "find me all numbers between x and y that are divisible by both 3 and 4." Later you state that you "input value 120 10 times". That is inconsistent with the problem so either the problem is stated incorrectly or you are not inputting the expected data. Decide what is the correct problem then write it up in pseudo-code before you write the actual code.


If a range is expected, make sure you test that it is a valid range (x <= y).
c = input()  

t = len(c)  

temp = ""  

for i in range(0,len(c)):  

    if t<3:  

        break  

    else:  

        if i in (0,1,t-2,t-1):  

            temp = temp+c[i]  

        else:  

            continue  

print(temp) Solution:


Hello Gavin,

You have got a nice question. No matter how the question is, don’t forget to ask. Thanks.  
There are a couple of ways to find the number in digits in a number. As you told you just started learning and well-known with the for-loop. I am trying to solve the problem using a simple iteration. Please have a look below program. I hope the program will clarify itself to you as you’ve covered the for-loop topic already.


```
public class DigitsInNumber{
    static int countDigit(long x){ 
        int counter = 0; 
        while (x != 0) { 
            x = x / 10; 
            ++counter; 
        } 
        return counter; 
    }

  	public static void main(String[] args){ 
        long x = 1052809467; 
        System.out.print("Number of digits : " + countDigit(x)); 
    } 
}
```
Thanks again. Good Day! (a) for digit



expression : 5n + 1

where n = no of digits

for 5 digits

no of segments required = 5(5) + 1 = 25 + 1 = 26

for 10 digits

no of segments required = 5(10) + 1 = 50 + 1 = 51

for 100 digits

no of segments required = 5(100) + 1 = 500 + 1 = 501

(b) for digit



expression : 3n + 1

where n = no of digits

for 5 digits

no of segments required = 3(5) + 1 = 15 + 1 = 16

for 10 digits

no of segments required = 3(10) + 1 = 30 + 1 = 31

for 100 digits

no of segments required = 3(100) + 1 = 300 + 1 = 301

(c) for digit



expression : 5n + 2

where n = no of digits

for 5 digits

no of segments required = 5(5) + 2 = 25 + 2 = 27

for 10 digits

no of segments required = 5(10) + 2 = 50 + 2 = 52

for 100 digits

no of segments required = 5(100) + 2 = 500 + 2 = 502 * If the number ends with a $0$ then there are $9$ choices for the first digit, $8$ for the second and $7$ for the third, which makes $1\times9\times8\times7= 504$ possibilities.
* If the number is even ending with something else than $0$ then there are $4$ choices for the last digit, $8$ choices for the first digit (no $0$ nor the last digit), $8$ for the second digit and $7$ for the third digit, which makes $4\times8\times8\times7 =1792$


Together, this gives $2296$ numbers with $4$ distinct digits that are even. Note that this does not allow leading $0$, as you see to want it based from the question.


Correct Answer: $B$ you can use the repmat function here
if you have to print the patter n (here 10) times, use the following code:
a = [5 4 3 2 1 0]';
n = 10;
b =repmat(a, n,1) The problem is that your pair's closure must return two different types of objects: int if you ask for "con", and the next closure on the line if you ask for "crd".


Here's one possible implementation:


Use a speaker so you can come back.



```
public static Func<string, dynamic> Pair(int x, Func<string, dynamic> y)
{
    Func<string, dynamic> pair =
           (a) =>
           {
               if (a == "con") return x;
               if (a == "crd") return y;
               throw new Exception();
           };

    return pair;
}

public static void Print(Func<string, dynamic> pair)
{
    while (true)
    {
        var next = pair("crd");
        Console.WriteLine((next == null ? "-":"+") +"> " + pair("con"));
        if (next != null)
        {
            Console.WriteLine("|");
            pair = (x) => next(x);
            continue;
        }
        break;
    }
}

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```


  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

Test:



```
var pair = Pair(1,
            Pair(2,
                  Pair(3,
                        Pair(4, null))));

Print(pair);

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```

output:



```
+> 1
|
+> 2
|
+> 3
|
-> 4

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
``` %%
var1= [1 1 2 4 5 7 7 7 8 10 11 13 14 14 17 17 17 17 18 20];
var2= [0.5 0.3 0.1 0.6 0.2 0.4 0.8 0.9 0.3 0.2 0.1 0.4 0.8 0.7 0.5 0.9 0.2 0.8 0.3 0.7];

x=min(var1):1:max(var1);
newele1=setdiff(x,var1);
newele2=nan(size(newele1));

newvar1=[var1,newele1];
newvar2=[var2,newele2];

[newvar1,index]=sort(newvar1);
newvar2=newvar2(index) Total no. of possible outcomes = 30 {1, 2, 3, … 30}  

  

E ⟶ event of getting no. divisible by 3.  

  

No. of favourable outcomes = 10 {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}  

  

Probability, P(E) = (No.of favorable outcomes)/(Total no.of possible outcomes)  

  

P(E) = 10/30 = 1/3  

  

Bar E ⟶ event of getting no. not divisible by 3.  

  

P(Bar E) = 1 – P(E)  

  

= 1 - 1/3 = 2/3 class intervals
frequency
(fi)
xi
di = xi - a
fi di


0 - 20
17
10
-40
-680


20 - 40
28
30
-20
-560


40 - 60
32
50 = a
0
0


60 - 80
p
70
20
20p


80 - 100
19
90
40
760


total
Î£
fi = 96 + p


Î£ fi di = 20p -
480


![](https://s3mn%20mnimgs%20com/img/shared/discuss_editlive/5130830/2013_07_08_13_01_36/mathmlequation8813644107607516415%20png) [image] wbriggs90:

for (int i = 0; i <= num\_leds; i++) {

this writes num\_leds+1 pixels, probably not what you wanted.

as the index starts with 0, the highest correct index would be num\_leds-1, not num\_leds.