## Learn Java Programming

An excellent way to Learn Java is to complement your reading and analysis with logical problem solving. We will share below some problems that you could practice your logical thinking with. We will also provide correct solution with explanations. Regular practice will make you a stronger programmer through time.

### Problem Set #1 - IF/ELSE Problems

Most of the things we need to do needs to branch out to different scenarios. If something is true then do this, otherwise do that. This is the exact use case of the if else statement - to have different branches of logic depending on some condition. Here are some problems to practice on.
Problem 1.1 - Find If Bigger Than 100
Given a number n, display if it is bigger than 100 or not. For numbers 101 and above, display "Bigger". No need to display anything for numbers 100 and below.
Examples:
• If n = 150, then display Bigger.
• If n = 40, then there should be no display.
• If n = 100, then there should be no display.
Problem 1.2 - Find If Bigger Or Not Bigger Than 100
Given a number n, display if it is bigger than 100 or not. For numbers 101 and above, display "Bigger". For numbers 100 and below, display "Not Bigger",
Examples:
• If n = 150, then display Bigger.
• If n = 40, then display Not Bigger.
• If n = 100, then display Not Bigger.
Problem 1.3 - Find The Bigger Number
Given two numbers a and b, display which of the two is bigger. In case of tie, just display the first number.
Examples:
• If a = 3 and b = 2, then display 3.
• If a = 5 and b = 7, then display 7.
• If a = 4 and b = 4, then display 4.
Problem 1.4 - Find The Smaller Number
Similarly, given two numbers a and b, display the smaller of the two. In case of tie, just display the first number.
Examples:
• If a = 3 and b = 2, then display 2.
• If a = 5 and b = 7, then display 5.
• If a = 4 and b = 4, then display 4.
Problem 1.5 - Find The Smaller and Bigger Number
Given two numbers a and b, display the smaller number first and then the bigger number second
Examples:
• If a = 3 and b = 2, then display 2 3.
• If a = 5 and b = 7, then display 5 7.
• If a = 4 and b = 4, then display 4 4.
• If a = 11 and b = 14, then display 11 14.
• If a = 15 and b = 8, then display 8 15.
Problem 1.6 - Find If Even
Given a number n, display if it is an even number. Even numbers are divisible by 2 - meaning if we divide the number by two, there should be no remainder. Examples of odd numbers are: 2, 4, 6, 8, 9, 10. The following are not even numbers: 1, 3, 5, 7, 9.
Examples:
• If n = 40, then display Even.
• If n = 41, then there should be no display.
• If n = 42, then display Even.
• If n = 43, then there should be no display.
Problem 1.7 - Find If Odd
Similarly, given a number n, display if it is an odd number. Odd numbers are not divisible by 2 - meaning if we divide the number by two, there should be a remainder 1. Examples of odd numbers are: 1, 3, 5, 7, 9. The following are not even numbers: 2, 4, 6, 8, 9, 10.
Examples:
• If n = 40, then there should be no display.
• If n = 41, then display Odd.
• If n = 42, then there should be no display.
• If n = 43, then display Odd.
Problem 1.8 - Find If Even or Odd
Given a number n, display if it is an odd number or an even number.
Examples:
• If n = 40, then display Even.
• If n = 41, then display Odd.
• If n = 42, then display Even.
• If n = 43, then display Odd.

### Solution To Problem Set #1

Problem 1.1 - Find If Bigger Than 100
For this problem, we will just compare the number n with 100 using the greater than operator. If n is greater than 100 then we display Bigger. The implementation is straightforward:
```if (n>100) {
System.out.println("Bigger");
}
```
Note that we don't display anything if the condition is not met as specified in the problem.
Problem 1.2 - Find If Bigger Or Not Bigger Than 100
For this problem, we again compare the number n with 100 using the greater than operator. If n is greater than 100 then we display Bigger. Otherwise we display Not Bigger. We use the else keyword to denote what should be done when the condition is not met.
```if (n>100) {
System.out.println("Bigger");
} else {
System.out.println("Not Bigger");
}
```
Problem 1.3 - Find The Bigger Number
We now make the problem slightly harder by comparing two arbitrary number. Again we use the greater than operator with an if statement. If a is greater than b, then we display a because a is bigger than b. Otherwise, we know that b is either equal or bigger than a. And so we display b if a is not greater than b.
```if (a>b) {
System.out.println(a);
} else {
System.out.println(b);
}
```
Problem 1.4 - Find The Smaller Number

Just for practice, we do the opposite condition. We use the less than operator and if statement in this case. If a is less than b then we display a because we know it is smaller than b. Otherwise we know b is less than a or at most equal than a. So we can confidently say b is smaller if the first condition is not met.

```if (a<b) {
System.out.println(a);
} else {
System.out.println(b);
}
```

Problem 1.5 - Find The Smaller and Bigger Number

Now we make it slightly harder than the last example. We need to display the smaller number first followed by the bigger one.

```if (a<b) {
System.out.println(a + " " + b);
} else {
System.out.println(b + " " + a);
}
```

As shown in the code above, if a is less than b then we know a is smaller than b, and so we display it first followed by b. Otherwise, we reverse the order because b is at least equal to a or even smaller.
Problem 1.6 - Find If Even
To solve this problem, we need to understand the operator % or modulo. What it does is give the remainder when we divide something. For example, n%2 will give the remainder if we divide n with 2.
Examples:
• 1%2 is 1
• 2%2 is 0
• 3%2 is 1
• 4%2 is 0
• 5%2 is 1
• 6%2 is 0
• 7%2 is 1
• 8%2 is 0
• 9%2 is 1
• 10%2 is 0
From the sample above, to know if something is even, the remainder should be zero. We translate that into code shown below:
```if (n%2==0) {
System.out.println("Even");
}
```
Problem 1.7 - Find If Odd
Similar to the Even problem in 1.6, we just check if remainder is 1 rather than 0. Hence the solution below:
```if (n%2==1) {
System.out.println("Odd");
}
```
Problem 1.8 - Find If Even or Odd
Solution to this is like combining what we learned in 1.2, 1.6 and 1.7. We just check if the remainder of n divided by 2 is 0 to know if we should display Even. if not met, we know it is odd.
```if (n%2==0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
```