easy
0 views
Print Unit Digit
Extract and print the rightmost digit (units place) of a given integer
Understand the Problem
Problem Statement
Given an integer N, print its unit digit (the digit in the ones place).
The unit digit is the remainder when the number is divided by 10.
Example:
For input 72, the unit digit is 2
For input 65, the unit digit is 5
Constraints
- The input number N can be any integer (positive, negative, or zero)
- The unit digit will be a single digit from 0 to 9
- Standard integer input/output operations apply
Examples
Example 1
Input
72Output
2Explanation
72 divided by 10 gives remainder 2, which is the unit digit
Example 2
Input
65Output
5Explanation
65 divided by 10 gives remainder 5, which is the unit digit
Example 3
Input
100Output
0Explanation
100 divided by 10 gives remainder 0, which is the unit digit
Solution
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
printf("%d", N % 10);
return 0;
}Time:O(1)
Space:O(1)
Approach:
C Solution Explanation:
- Include standard input/output header file
- Declare integer variable N to store input
- Use scanf to read the integer from standard input
- Use printf to print N % 10, which gives the unit digit
- Return 0 to indicate successful execution
Visual Explanation
Loading diagram...