Sum of two numbers modulo M
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) % 3 = 30 % 3 = 0
Input: a = 100, b = 13, m = 107
Output: 6
Approach: To solve the problem follow the below idea:
Add the two given numbers A and B and print their sum under modulo M.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sum( int a, int b, int m)
{
int s = a + b;
s = s % m;
return s;
}
int main()
{
int a = 20, b = 10, m = 3;
cout << sum(a, b, m);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sum( int a, int b, int m)
{
int s = a + b;
s = s % m;
return s;
}
public static void main(String[] args)
{
int a = 10 , b = 20 , m = 3 ;
System.out.println( "The sum = " + sum(a, b, m));
}
}
|
Python
def summ(a, b, m):
s = a + b
s = s % m
return s
if __name__ = = '__main__' :
a = 20
b = 10
m = 3
print summ(a, b, m)
|
C#
using System;
class GFG {
static int sum( int a, int b, int m)
{
int s = a + b;
s = s % m;
return s;
}
public static void Main()
{
int a = 10, b = 20, m = 3;
Console.Write( "The sum = " + sum(a, b, m));
}
}
|
PHP
<?php
function sum( $a , $b , $m )
{
$s = $a + $b ;
$s = $s % $m ;
return $s ;
}
$a = 20;
$b = 10;
$m = 3;
echo (sum( $a , $b , $m ));
?>
|
Javascript
<script>
function sum( a, b, m)
{
let s = a + b;
s = s % m;
return s;
}
let a = 20, b = 10, m = 3;
document.write(sum(a, b, m));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...