Remainder Evaluation
Last Updated :
24 Mar, 2023
Given two positive integers Num1 and Num2, the task is to find the remainder when Num1 is divided by Num2.
Examples:
Input: Num1 = 11, Num2 = 3
Output: 2
Explanation: 3) 11 (3
– 9
———
2 -> Remainder
———-
Input: Num1 = 15, Num2 = 3
Output: 0
Approach 1: The problem can be solved by using the modulus operator.
- Modulus operator returns the remainder, if we write a % b, it returns the remainder when a is divided by b where b != 0. If b = 0, then it gives Runtime Error,
- Math error in C++, (Math error: Attempted to divide by Zero)
- ZeroDivisionError in Python, [ZeroDivisionError: integer division or modulo by zero]
- ArithmeticException in Java [ArithmeticException: / by zero]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int Num1, int Num2)
{
return Num1 % Num2;
}
int main()
{
int Num1 = 11;
int Num2 = 3;
cout << solve(Num1, Num2) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int solve( int Num1, int Num2)
{
return Num1 % Num2;
}
public static void main(String[] args)
{
int Num1 = 11 ;
int Num2 = 3 ;
System.out.println(solve(Num1, Num2));
}
}
|
Python3
def solve(Num1, Num2):
return Num1 % Num2
Num1 = 11
Num2 = 3
print (solve(Num1, Num2))
|
C#
using System;
class GFG
{
public static int solve( int Num1, int Num2)
{
return Num1 % Num2;
}
public static void Main()
{
int Num1 = 11;
int Num2 = 3;
Console.Write(solve(Num1, Num2));
}
}
|
Javascript
<script>
function solve(Num1, Num2) {
return Num1 % Num2;
}
let Num1 = 11;
let Num2 = 3;
document.write(solve(Num1, Num2));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach 2: Without using the modulus (%) operator
In this approach, we will consider Num2 as the divider and Num1 as the Dividend. so Quotient will be Num1 / Num2. then we will subtract (Quotient * Num2) from Num1, and this will be the Remainder.
Quotient = Num1 / Num2
Reminder = Num1 - (Quotient * Num2)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int Num1, int Num2)
{
return Num1 - ((Num1 / Num2) * Num2);
}
int main()
{
int Num1 = 11;
int Num2 = 3;
cout << solve(Num1, Num2) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int solve( int Num1, int Num2)
{
return Num1 - ((Num1 / Num2) * Num2);
}
public static void main(String[] args)
{
int Num1 = 11 ;
int Num2 = 3 ;
System.out.println(solve(Num1, Num2));
}
}
|
Python3
def solve(Num1, Num2):
return Num1 - ( int (Num1 / Num2) * Num2);
Num1 = 11
Num2 = 3
print ( int (solve(Num1, Num2)))
|
C#
using System;
public class GFG {
public static int solve( int Num1, int Num2)
{
return Num1 - ((Num1 / Num2) * Num2);
}
static public void Main()
{
int Num1 = 11;
int Num2 = 3;
Console.WriteLine(solve(Num1, Num2));
}
}
|
Javascript
<script>
function solve( Num1,Num2)
{
return Num1 - (Math.floor(Num1 / Num2) * Num2);
}
let Num1 = 11;
let Num2 = 3;
console.log(solve(Num1, Num2));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...