Skip to content
Related Articles

Related Articles

Sum of two numbers modulo M
  • Last Updated : 02 Oct, 2020

Given three numbers A, B, and M. The task is to print the sum of A and B under modulo M. 

Examples

Input: 
a = 10, b = 20, m = 3 
Output: 
0
Explanation:
10+20 = 30 % 3 = 0 

Input: 
a = 100, b = 13, m = 107 
Output: 
6

Approach: 

Add the two given numbers A and B and print their sum under modulo M.

Below is the implementation of the above approach:  

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for sum of two
// numbers modulo M
#include <iostream>
using namespace std;
 
// Function to return summation mod m
int sum(int a, int b, int m)
{
    // add two numbers
    int s = a + b;
 
    // do a mod with m
    s = s % m;
 
    return s;
}
 
// Driver Code
int main()
{
    int a = 20, b = 10, m = 3;
   
      // Function Call
    cout << sum(a, b, m);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA program for addition of
// two numbers modulo m
 
import java.io.*;
 
class GFG {
 
    static int sum(int a, int b, int m)
    {
        // add two numbers
        int s = a + b;
 
        // do mod with m
        s = s % m;
        return s;
    }
    // Driver Code
    public static void main(String[] args)
    {
        int a = 10, b = 20, m = 3;
 
        // Function Call
        System.out.println("The sum = " + sum(a, b, m));
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program for addition of
# two numbers modulo m
 
 
def summ(a, b, m):
 
    # add two number
    s = a + b
 
    # do mod with m
    s = s % m
    return s
 
 
# Driver Code
a = 20
b = 10
m = 3
 
# Function Call
print summ(a, b, m)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for addition of
// two numbers modulo m
using System;
 
class GFG {
 
    static int sum(int a, int b, int m)
    {
 
        // add two numbers
        int s = a + b;
 
        // do mod with m
        s = s % m;
        return s;
    }
 
    // Driver Code
    public static void Main()
    {
        int a = 10, b = 20, m = 3;
 
        // Function Call
        Console.Write("The sum = " + sum(a, b, m));
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Php program for sum of two
// numbers modulo M
 
// Function to return summation mod m
function sum($a, $b, $m)
{
    // add two numbers
    $s = $a + $b;
 
    // do a mod with m
    $s = $s % $m;
 
    return $s;
}
 
// Driver Code
$a = 20;
$b = 10;
$m = 3;
 
// Function Call
echo (sum($a, $b, $m));
 
// This code is contributed
// by Shivi_Aggarwal
?>

chevron_right


Output

0



My Personal Notes arrow_drop_up
Recommended Articles
Page :