Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Remainder Evaluation

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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++




// C++ code to implement the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the remainder
// when Num1 is divided by Num2
int solve(int Num1, int Num2)
{
    return Num1 % Num2;
}
  
// Driver Code
int main()
{
    int Num1 = 11;
    int Num2 = 3;
  
    // Function Call
    cout << solve(Num1, Num2) << endl;
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
  
class GFG {
    // Function to find the remainder
    // when Num1 is divided by Num2
    public static int solve(int Num1, int Num2)
    {
        return Num1 % Num2;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int Num1 = 11;
        int Num2 = 3;
  
        // Function Call
        System.out.println(solve(Num1, Num2));
    }
}
  
// This code is contributed by Rohit Pradhan


Python3




# Python3 code to implement the approach
  
# Function to find the remainder
# when Num1 is divided by Num2
def solve(Num1, Num2):
    return Num1 % Num2
  
# Driver Code
Num1 = 11
Num2 = 3
  
# Function Call
print(solve(Num1, Num2))
  
# This code is contributed by akashish__


C#




// C# program to implement
// the above approach
using System;
class GFG
{
  
    // Function to find the remainder
    // when Num1 is divided by Num2
    public static int solve(int Num1, int Num2)
    {
        return Num1 % Num2;
    }
  
// Driver Code
public static void Main()
{
    int Num1 = 11;
    int Num2 = 3;
  
    // Function Call
    Console.Write(solve(Num1, Num2));
}
}
  
// This code is contributed by sanjoy_62.


Javascript




<script>
        // JS code to implement the approach
  
        // Function to find the remainder
        // when Num1 is divided by Num2
        function solve(Num1, Num2) {
            return Num1 % Num2;
        }
  
        // Driver Code
        let Num1 = 11;
        let Num2 = 3;
  
        // Function Call
        document.write(solve(Num1, Num2));
  
// This code is contributed by lokeshpotta20.
    </script>


Output

2

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++




// C++ code to implement the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the remainder when Num1 is divided by
// Num2 without using % operator
int solve(int Num1, int Num2)
{
    return Num1 - ((Num1 / Num2) * Num2);
}
  
// Driver Code
int main()
{
    int Num1 = 11;
    int Num2 = 3;
  
    // Function Call
    cout << solve(Num1, Num2) << endl;
    return 0;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
  
class GFG {
  
  // Function to find the remainder when Num1 is divided
  // by Num2 without using % operator
  static int solve(int Num1, int Num2)
  {
    return Num1 - ((Num1 / Num2) * Num2);
  }
  
  public static void main(String[] args)
  {
    int Num1 = 11;
    int Num2 = 3;
  
    // Function Call
    System.out.println(solve(Num1, Num2));
  }
}
  
// This code is contributed by aadityaburujwale.


Python3




# Python3 code to implement the above approach
  
# Function to find the remainder when Num1 is divided by
# Num2 without using % operator
def solve(Num1, Num2):
  return Num1 - (int(Num1 / Num2) * Num2);
  
# Driver Code
Num1 = 11
Num2 = 3
  
# Function Call
print(int(solve(Num1, Num2)))
  
# This code is contributed by akashish__


C#




using System;
  
public class GFG {
  
  // Function to find the remainder when Num1 is divided
  // by
  // Num2 without using % operator
  public static int solve(int Num1, int Num2)
  {
    return Num1 - ((Num1 / Num2) * Num2);
  }
  
  static public void Main()
  {
  
    int Num1 = 11;
    int Num2 = 3;
  
    // Function Call
    Console.WriteLine(solve(Num1, Num2));
  }
}
  
// This code is contributed by akashish__.


Javascript




<script>
  
// Function to find the remainder when Num1 is divided by
// Num2 without using % operator
function solve( Num1,Num2)
{
    return Num1 - (Math.floor(Num1 / Num2) * Num2);
}
  
// Driver Code
let Num1 = 11;
let Num2 = 3;
  
// Function Call
console.log(solve(Num1, Num2));
  
// This code is contributed by akashish__
  
</script>


Output

2

Time Complexity: O(1)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Last Updated : 24 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials