Now let's take a few examples (exercises) to learn how to use
if-else conditional statements in practice.
Watch the video lesson about the "Even or Odd Number" and "The Larger Number" problems and their solutions: https://youtu.be/qIUoKiObr-A.
Write a program that checks whether an integer is even or odd.
We can solve the problem with one
if-else statement and the operator
%, which returns a remainder by dividing two numbers.
Test your solution here:
Write a program that reads from the console two integers and prints the larger of them. Print "Greater number: " + the bigger number.
The input comes as two numbers, each on a separate line. Sample input:
The output is a message like the shown below. Sample output:
Greater number: 5
Our first task is to read the two numbers from the console. Then, with a simple
if-else statement, combined with the operator for greater than (
>), we do check. Part of the code is deliberately blurred, so you can test what you learned so far.
Test your solution here: https://judge.softuni.org/Contests/Practice/Index/506#3.