Abdelrahman Mohamed - 5 months ago 49

Java Question

I'm totally new to Java, and I'm facing a little problem. Sometimes I need to declare a numerical variable in between two values like

`int x>=2 && x<=20;`

For example I have this problem to solve:

Given an integer, N, print its first 10 multiples. Each multiple N*i (where 1 <= i <= 10) should be printed on a new line in the form: N x i = result.

Constraints

2 <= N <= 20

How can i solve that, and how can I initialize an int variable "in between two values" ?

Answer

It makes no sense to "initialize a variable between 2 values", as a variable can have only 1 value at a time.

You should give the different possible values to the variable one by one :

```
int i=1;
while(i<=10){
System.out.println("N x " + i + " = " + (N*i));
i++;
}
```

If you don't want to keep your `i`

variable after you can use a `for`

loop:

```
for(int i=1; i<= 10; i++){
System.out.println("N x "+ i +" = " + (N*i));
}
```

You can do the same if you want to do this for all the specified values of N:

```
for(int N = 2; N <= 20; N++)
for(int i = 1; i <= 10; i++)
System.out.println("N x "+ i +" = " + (N*i));
```

In software programming, the very concept of a variable is a "placeholder to store a value". So by definition, a variable can hold only one value, and is doing so at any time.

When you see variables that seem to hold several values, they are actually holding 1 value, which itself is a container for several values.