Operators Practice
Arithmetic, comparison, logical, assignment, bitwise, and membership operators in Python.
1
Last Digit of a Number
Easy
Given an integer N, print its last digit (units digit).
Input Format
A single integer N.
Output Format
Print the last digit of N.
Sample Test Cases
|
Sample Input 1
237 |
Sample Output 1
7 |
|
Sample Input 2
0 |
Sample Output 2
0 |
|
Sample Input 3
1000000000 |
Sample Output 3
0 |
Constraints
0 ≤ N ≤ 10^9
Explanation
Use the modulo operator: N % 10 gives the last digit.
2
Check Divisibility
Easy
Given two integers A and B, check if A is divisible by B.
Input Format
Two space-separated integers A and B.
Output Format
Print "Divisible" if A is divisible by B, otherwise print "Not Divisible".
Sample Test Cases
|
Sample Input 1
15 4 |
Sample Output 1
Not Divisible |
|
Sample Input 2
20 5 |
Sample Output 2
Divisible |
|
Sample Input 3
7 2 |
Sample Output 3
Not Divisible |
Constraints
1 ≤ A, B ≤ 10^6
Explanation
Use the modulo operator %: if A % B == 0 then A is divisible by B.
3
Find the Remainder
Easy
Given two integers A and B, find the remainder when A is divided by B.
Input Format
Two space-separated integers A and B.
Output Format
Print the remainder as an integer.
Sample Test Cases
|
Sample Input 1
17 5 |
Sample Output 1
2 |
|
Sample Input 2
100 7 |
Sample Output 2
2 |
|
Sample Input 3
5 2 |
Sample Output 3
1 |
Constraints
1 ≤ A, B ≤ 10^6
Explanation
Use the modulo operator: A % B.
4
Average of Three Numbers
Easy
Compute average of three numbers.
Input Format
Three ints A B C
Output Format
Average rounded to 2 dp.
Sample Test Cases
|
Sample Input 1
10 20 30 |
Sample Output 1
20.00 |
|
Sample Input 2
5 5 5 |
Sample Output 2
5.00 |
|
Sample Input 3
-1 0 1 |
Sample Output 3
0.00 |
Constraints
1≤A,B,C≤10⁶
Explanation
Sum/3
5
Check Odd or Even
Easy
Use modulo operator.
Input Format
Single int N
Output Format
"Even" or "Odd"
Sample Test Cases
|
Sample Input 1
7 |
Sample Output 1
Odd |
|
Sample Input 2
0 |
Sample Output 2
Even |
|
Sample Input 3
1000000000 |
Sample Output 3
Even |
Constraints
1≤N≤10⁹
Explanation
N%2==0 means even.
6
Power of a Number
Easy
Compute B^E using operator.
Input Format
Two ints B E
Output Format
Print B^E result.
Sample Test Cases
|
Sample Input 1
2 10 |
Sample Output 1
1024 |
|
Sample Input 2
3 3 |
Sample Output 2
27 |
|
Sample Input 3
10 0 |
Sample Output 3
1 |
Constraints
1≤B≤10, 0≤E≤15
Explanation
Use ** operator.
7
Floor Division
Easy
Integer division A//B
Input Format
Two ints A B
Output Format
Print A//B.
Sample Test Cases
|
Sample Input 1
25 4 |
Sample Output 1
6 |
|
Sample Input 2
100 7 |
Sample Output 2
14 |
|
Sample Input 3
5 5 |
Sample Output 3
1 |
Constraints
1≤B≤A≤10⁶
Explanation
Use //
8
Increment and Decrement
Easy
Print N+1 and N-1 using += and -=.
Input Format
Single int N
Output Format
Two lines: N+1, N-1
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
11 9 |
|
Sample Input 2
0 |
Sample Output 2
1 -1 |
|
Sample Input 3
-1000000 |
Sample Output 3
-999999 -1000001 |
Constraints
-10⁶≤N≤10⁶
Explanation
Use += and -=
9
Check Greater
Easy
Is A > B using > operator?
Input Format
Two ints A B
Output Format
"Yes" or "No"
Sample Test Cases
|
Sample Input 1
15 10 |
Sample Output 1
Yes |
|
Sample Input 2
5 5 |
Sample Output 2
No |
|
Sample Input 3
-1 1 |
Sample Output 3
No |
Constraints
-10⁶≤A,B≤10⁶
Explanation
Use >
10
Tens Digit
Easy
Extract tens place digit using // and %.
Input Format
Single int N
Output Format
Print tens digit.
Sample Test Cases
|
Sample Input 1
345 |
Sample Output 1
4 |
|
Sample Input 2
100 |
Sample Output 2
0 |
|
Sample Input 3
1000000 |
Sample Output 3
0 |
Constraints
10≤N≤10⁶
Explanation
(N//10)%10
1
Check Leap Year with Logical Operators
Medium
Given a year Y, determine if it is a leap year using logical operators. A year is a leap year if it is divisible by 400 OR (divisible by 4 AND not divisible by 100).
Input Format
A single integer Y.
Output Format
Print "Leap Year" or "Not a Leap Year".
Sample Test Cases
|
Sample Input 1
1900 |
Sample Output 1
Not a Leap Year |
|
Sample Input 2
2000 |
Sample Output 2
Leap Year |
|
Sample Input 3
2024 |
Sample Output 3
Leap Year |
Constraints
1000 ≤ Y ≤ 3000
Explanation
Use logical operators: Y % 400 == 0 or (Y % 4 == 0 and Y % 100 != 0)
2
Swap Bits (Bitwise XOR)
Medium
Given two integers X and Y, swap their values without using a third variable. Use only bitwise XOR operators.
Input Format
Two space-separated integers X and Y.
Output Format
Print the swapped values in the format: "X = [newX], Y = [newY]"
Sample Test Cases
|
Sample Input 1
9 14 |
Sample Output 1
X = 14, Y = 9 |
|
Sample Input 2
5 12 |
Sample Output 2
X = 12, Y = 5 |
|
Sample Input 3
100 57 |
Sample Output 3
X = 57, Y = 100 |
Constraints
1 ≤ X, Y ≤ 10^6
Explanation
X = X ^ Y; Y = X ^ Y; X = X ^ Y. XOR has the property that a ^ b ^ b = a.
3
Power of Two Check
Medium
Is N a power of 2? Use (N & (N-1))==0
Input Format
Single int N
Output Format
"Power of Two" or "Not"
Sample Test Cases
|
Sample Input 1
16 |
Sample Output 1
Power of Two |
|
Sample Input 2
1 |
Sample Output 2
Power of Two |
|
Sample Input 3
100 |
Sample Output 3
Not |
Constraints
1≤N≤10⁹
Explanation
Use bitwise AND.
4
Evaluate Expression
Medium
(A+B)*(C-D)/E with floats
Input Format
Five ints A B C D E
Output Format
Result rounded to 2 dp.
Sample Test Cases
|
Sample Input 1
10 5 20 7 4 |
Sample Output 1
48.75 |
|
Sample Input 2
1 1 5 2 2 |
Sample Output 2
3.00 |
|
Sample Input 3
3 4 6 2 10 |
Sample Output 3
2.80 |
Constraints
C>D, E≠0
Explanation
Follow precedence.
5
Nth Bit Check
Medium
Check if K-th bit of N is set using (1
Input Format
Two ints N K
Output Format
"Set" or "Not Set"
Sample Test Cases
|
Sample Input 1
13 2 |
Sample Output 1
Set |
|
Sample Input 2
13 3 |
Sample Output 2
Set |
|
Sample Input 3
0 0 |
Sample Output 3
Not Set |
Constraints
0≤N≤10⁹, 0≤K≤31
Explanation
N & (1
6
Toggle Bits
Medium
Toggle all bits after MSB using XOR.
Input Format
Single int N
Output Format
Result integer.
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
5 |
|
Sample Input 2
1 |
Sample Output 2
0 |
|
Sample Input 3
31 |
Sample Output 3
0 |
Constraints
1≤N≤10⁶
Explanation
Mask and XOR.
7
Right Shift Count
Medium
Count right shifts needed to reach 0 (N>>1).
Input Format
Single int N
Output Format
Count of shifts.
Sample Test Cases
|
Sample Input 1
8 |
Sample Output 1
4 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
1024 |
Sample Output 3
11 |
Constraints
1≤N≤10⁹
Explanation
Count till N==0.
8
Absolute Value
Medium
Absolute value without abs(), using conditionals.
Input Format
Single int N
Output Format
Print absolute value.
Sample Test Cases
|
Sample Input 1
-15 |
Sample Output 1
15 |
|
Sample Input 2
0 |
Sample Output 2
0 |
|
Sample Input 3
1000000000 |
Sample Output 3
1000000000 |
Constraints
-10⁹≤N≤10⁹
Explanation
If N
9
Same Sign Check
Medium
Do A and B have same sign?
Input Format
Two ints A B
Output Format
"Same Sign" or "Different Sign"
Sample Test Cases
|
Sample Input 1
-5 -10 |
Sample Output 1
Same Sign |
|
Sample Input 2
0 0 |
Sample Output 2
Same Sign |
|
Sample Input 3
-1 1 |
Sample Output 3
Different Sign |
Constraints
-10⁶≤A,B≤10⁶
Explanation
Check >=0 conditions.
10
BMI Calculator
Medium
BMI = weight / height²
Input Format
Two floats: weight(kg) height(m)
Output Format
BMI rounded 2 dp.
Sample Test Cases
|
Sample Input 1
72 1.75 |
Sample Output 1
23.51 |
|
Sample Input 2
100 2.0 |
Sample Output 2
25.00 |
|
Sample Input 3
50 1.6 |
Sample Output 3
19.53 |
Constraints
20≤w≤200, 1≤h≤2.5
Explanation
Use ** for square.
1
Count Set Bits
Hard
Given an integer N, count the number of 1 bits (set bits) in its binary representation.
Input Format
A single integer N.
Output Format
Print the count of set bits in the binary representation of N.
Sample Test Cases
|
Sample Input 1
13 |
Sample Output 1
3 |
|
Sample Input 2
255 |
Sample Output 2
8 |
|
Sample Input 3
7 |
Sample Output 3
3 |
Constraints
1 ≤ N ≤ 10^9
Explanation
Binary of 13 is 1101, which has three 1s. Use N & (N-1) trick or bitwise shift.
2
Gray Code Converter
Hard
Convert binary string to Gray code (XOR each bit with previous).
Input Format
Binary string (0s/1s)
Output Format
Gray code binary string.
Sample Test Cases
|
Sample Input 1
1011 |
Sample Output 1
1110 |
|
Sample Input 2
1000 |
Sample Output 2
1100 |
|
Sample Input 3
1111 |
Sample Output 3
1000 |
Constraints
Len≤32
Explanation
MSB same, Gray[i]=bin[i]^bin[i-1]
3
Missing Number XOR
Hard
Find missing number from 1..N using XOR.
Input Format
Line1: N. Line2: N-1 ints
Output Format
Print missing number.
Sample Test Cases
|
Sample Input 1
5 3 1 5 2 |
Sample Output 1
4 |
|
Sample Input 2
3 1 2 |
Sample Output 2
3 |
|
Sample Input 3
4 4 3 2 |
Sample Output 3
1 |
Constraints
2≤N≤10⁵
Explanation
XOR all 1..N with array.
4
Postfix Evaluation
Hard
Evaluate RPN expression (+,-,*,/). Single-digit operands.
Input Format
Space-separated postfix.
Output Format
Integer result.
Sample Test Cases
|
Sample Input 1
5 3 + 8 * 4 / |
Sample Output 1
16 |
|
Sample Input 2
3 4 + 2 * |
Sample Output 2
14 |
|
Sample Input 3
8 2 / 4 * |
Sample Output 3
16 |
Constraints
Expr≤100 chars
Explanation
Stack-based.
5
Two Unique Numbers
Hard
Array where two numbers appear once, rest twice. Find them using XOR.
Input Format
Line1: N. Line2: N ints
Output Format
Two unique numbers asc.
Sample Test Cases
|
Sample Input 1
8 1 2 3 2 1 4 5 5 |
Sample Output 1
3 4 |
|
Sample Input 2
6 1 1 2 2 3 4 |
Sample Output 2
3 4 |
|
Sample Input 3
4 5 6 5 6 |
Sample Output 3
0 0 |
Constraints
2≤N≤10⁵
Explanation
XOR all -> find set bit, partition.
6
Max AND in Range
Hard
Max value of A AND B for L≤A
Input Format
Two ints L R
Output Format
Max AND value.
Sample Test Cases
|
Sample Input 1
2 8 |
Sample Output 1
6 |
|
Sample Input 2
5 7 |
Sample Output 2
6 |
|
Sample Input 3
8 16 |
Sample Output 3
14 |
Constraints
1≤L
Explanation
Find MSB where L and R differ.
7
Triplet Unique
Hard
Every element appears 3x except one. Find it using bitwise.
Input Format
Line1: N. Line2: N ints
Output Format
Print unique element.
Sample Test Cases
|
Sample Input 1
10 3 5 3 3 2 5 5 7 2 2 |
Sample Output 1
7 |
|
Sample Input 2
7 1 1 1 2 2 2 3 |
Sample Output 2
3 |
|
Sample Input 3
4 10 10 10 5 |
Sample Output 3
5 |
Constraints
4≤N≤10⁵
Explanation
ones/twos bit tracking.
8
Square Root Binary
Hard
Floor sqrt(N) without sqrt() or **0.5, using binary search.
Input Format
Single int N
Output Format
Floor of sqrt.
Sample Test Cases
|
Sample Input 1
50 |
Sample Output 1
7 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
1000000000 |
Sample Output 3
31622 |
Constraints
1≤N≤10⁹
Explanation
Binary search with *.
9
Bit Palindrome
Hard
Is binary representation a palindrome?
Input Format
Single int N
Output Format
"Bit Palindrome" or "Not"
Sample Test Cases
|
Sample Input 1
9 |
Sample Output 1
Bit Palindrome |
|
Sample Input 2
5 |
Sample Output 2
Bit Palindrome |
|
Sample Input 3
12 |
Sample Output 3
Not |
Constraints
1≤N≤10⁶
Explanation
Extract bits, compare.
10
Next Power of 2
Hard
Smallest power of 2 ≥ N using bitwise.
Input Format
Single int N
Output Format
Next power of 2.
Sample Test Cases
|
Sample Input 1
25 |
Sample Output 1
32 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
1000000000 |
Sample Output 3
1073741824 |
Constraints
1≤N≤10⁹
Explanation
Decrement, set bits via OR/shifts.