The Wayback Machine - https://web.archive.org/web/20240713132903/https://www.geeksforgeeks.org/remainder-evaluation/
Open In App

Remainder Evaluation

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)



Similar Reads

Expression Evaluation
Evaluate an expression represented by a String. The expression can contain parentheses, you can assume parentheses are well-matched. For simplicity, you can assume only binary operations allowed are +, -, *, and /. Arithmetic Expressions can be written in one of three forms: Infix Notation: Operators are written between the operands they operate on
15 min read
Evaluation of Prefix Expressions
Prefix and Postfix expressions can be evaluated faster than an infix expression. This is because we don't need to process any brackets or follow operator precedence rule. In postfix and prefix expressions which ever operator comes before will be evaluated first, irrespective of its priority. Also, there are no brackets in these expressions. As long
13 min read
Evaluation of Risk in Investments
Given two investment options A and B, we have to find the less risky investment of the two. The two investments A and B are each represented by an array. Each element in the array is a probable investment outcome. Thus each element in the array is a pair of two values. The first value is the amount of money received and the second value is the prob
12 min read
Arithmetic Expression Evaluation
The stack organization is very effective in evaluating arithmetic expressions. Expressions are usually represented in what is known as Infix notation, in which each operator is written between two operands (i.e., A + B). With this notation, we must distinguish between ( A + B )*C and A + ( B * C ) by using either parentheses or some operator-preced
2 min read
Evaluation of Postfix Expression
Given a postfix expression, the task is to evaluate the postfix expression. Postfix expression: The expression of the form "a b operator" (ab+) i.e., when a pair of operands is followed by an operator. Examples: Input: str = "2 3 1 * + 9 -"Output: -4Explanation: If the expression is converted into an infix expression, it will be 2 + (3 * 1) - 9 = 5
15+ min read
Grid Evolution and Query Evaluation
Given a grid of dimensions n rows and m columns, where each element initially contains an integer from 0 to k-1. The grid evolves over time, with each element undergoing the transformation (grid[i][j] + 1) % k after each unit of time. You are also provided with a series of queries, each defined by [t, val, lx, ly, rx, ry], the task is to determine,
13 min read
Horner's Method for Polynomial Evaluation
Given a polynomial of the form cnxn + cn-1xn-1 + cn-2xn-2 + ... + c1x + c0 and a value of x, find the value of polynomial for a given value of x. Here cn, cn-1, .. are integers (may be negative) and n is a positive integer.Input is in the form of an array say poly[] where poly[0] represents coefficient for xn and poly[1] represents coefficient for
6 min read
Introduction to Evaluation Function of Minimax Algorithm in Game Theory
Prerequisite: Minimax Algorithm in Game TheoryAs seen in the above article, each leaf node had a value associated with it. We had stored this value in an array. But in the real world when we are creating a program to play Tic-Tac-Toe, Chess, Backgammon, etc. we need to implement a function that calculates the value of the board depending on the pla
8 min read
Evaluation of Expression Tree
Given a simple expression tree, consisting of basic binary operators i.e., + , - ,* and / and some integers, evaluate the expression tree. Examples: Input: Root node of the below tree Output:100 Input: Root node of the below tree Output: 110 Recommended PracticeExpression TreeTry It! Approach: The approach to solve this problem is based on followin
9 min read
All ways to add parenthesis for evaluation
Given a string that represents an expression constituting numbers and binary operator +, - and * only. We need to parenthesize the expression in all possible way and return all evaluated values. Input : expr = “3-2-1”Output : {0, 2}((3-2)-1) = 0 (3-(2-1)) = 2Input : expr = "5*4-3*2"Output : {-10, 10, 14, 10, 34}(5*(4-(3*2))) = -10(5*((4-3)*2)) = 10
8 min read
Practice Tags :
three90RightbarBannerImg