easy
0 views

Print Remainder when Divided by 5

Calculate and print the remainder when a given integer is divided by 5

Understand the Problem

Problem Statement

The program must accept a number and print its remainder when it is divided by 5.

For example, if the input is 12, the output should be 2 because 12 % 5 = 2.

Constraints

  • The input number N is an integer
  • N can be positive, negative, or zero
  • Standard integer range constraints apply for the programming language used

Examples

Example 1
Input
12
Output
2
Explanation

When 12 is divided by 5, the quotient is 2 and the remainder is 2 (since 12 = 5 × 2 + 2).

Example 2
Input
10
Output
0
Explanation

When 10 is divided by 5, the quotient is 2 and the remainder is 0 (since 10 = 5 × 2 + 0).

Example 3
Input
7
Output
2
Explanation

When 7 is divided by 5, the quotient is 1 and the remainder is 2 (since 7 = 5 × 1 + 2).

Example 4
Input
-3
Output
2
Explanation

In most programming languages, -3 % 5 = 2 (since -3 = 5 × (-1) + 2).

Solution

#include <stdio.h>

int main() {
    int N;
    scanf("%d", &N);
    printf("%d\n", N % 5);
    return 0;
}
Time:O(1) - The modulo operation is a constant time operation
Space:O(1) - Only uses a single integer variable
Approach:

Step 1: Include the standard input/output header file stdio.h

Step 2: Declare an integer variable N to store the input

Step 3: Use scanf to read the integer from standard input

Step 4: Calculate N % 5 to find the remainder

Step 5: Use printf to output the result followed by a newline

Step 6: Return 0 to indicate successful execution

Visual Explanation

Loading diagram...