Decrement OR Increment Codechef Solution

Decrement OR Increment Codechef Solution

Problem

Write a program to obtain a number N and increment its value by 1 if the number is divisible by 4 otherwise decrement its value by 1.

Input Format:

  • First line will contain a number N.

Output Format:

Output a single line, the new value of the number.

Constrains

  • 0 ≤ N ≤ 1000

Sample Input 

5

Sample Output 

4

Explanation:

Since 5 is not divisible by 4 hence, its value is decreased by 1.

Decrement OR Increment – CodeChef Solution in Python

a = int(input())
if (a % 4 == 0):
    a = a + 1
    print(int(a))
else:
    a = a - 1
    print(int(a))

Decrement OR Increment – CodeChef Solution in CPP

#include <stdio.h>
int main(void) {
    int N;
    scanf("%d",&N);
    if(N%4==0)
    N=N+1;
    else
    N=N-1;
    printf("%d",N);
	// your code goes here
	return 0;
}

Decrement OR Increment – CodeChef Solution in JAVA

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		System.out.println(solve(N));
		sc.close();
	}
	static int solve(int N) {
		return (N % 4 == 0) ? (N + 1) : (N - 1);
	}
}

Disclaimer: The above Problem (Number Mirror) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment