Next even integer#8888

Next even integer (Editorial)

Increase the number by 1. If the resulting number is even, then it is the answer. Otherwise, increase again by 1.

Example

In the first example n = 7. We increase it by 1: n = 8. The number is even, so it is the answer.

In the second example n = 4. We increase it by 1: n = 5. The number is odd, so we increase it again by 1. We get n = 6, which is the answer.

Algorithm Implementation

Read the input number .

scanf("%d", &n);

Increase the number by 1.

n++;

If is odd, then increase it again by 1.

if (n % 2 != 0) n++;

Print the answer.

printf("%d\n", n);

Java Implementation

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner con = new Scanner(System.in);
        int n = con.nextInt();
        n++;
        if (n % 2 != 0) n++;
        System.out.println(n);    
        con.close();
    }
}

Python Implementation

Read the input number .

n = int(input())

Increase the number by 1.

n += 1

If is odd, then increase it again by 1.

if n % 2 != 0: n += 1

Print the answer.

print(n)