Sum of two numbers modulo M Last Updated : 13 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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: 0Explanation: (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++ // C++ program for sum of two // numbers modulo M #include <bits/stdc++.h> 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; } Java // 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)); } } Python # 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 if __name__ == '__main__': a = 20 b = 10 m = 3 # Function Call print summ(a, b, m) C# // 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 PHP <?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 ?> JavaScript <script> // Javascript program for sum of two // numbers modulo M // Function to return summation mod m function sum( a, b, m) { // add two numbers let s = a + b; // do a mod with m s = s % m; return s; } // driver code let a = 20, b = 10, m = 3; // Function Call document.write(sum(a, b, m)); // This code is contributed by Jana_Sayantan. </script> Output0 Time complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Sum of two numbers modulo M A AnkanDas22 Follow Improve Article Tags : DSA Similar Reads Sum of two large numbers as Strings Given two numbers as strings. The numbers may be very large (may not fit in long long int), the task is to find sum of these two numbers.Examples: Input: s1 = "23", s2 = "25"Output: "48"Input: s1 = "00", s2 = "000"Output: "0"Input: s1 = "10000000", s2 = "89990000"Output: 99990000One by One Adding Di 7 min read Sum of multiples of a number up to N Given a number a and limit N. Find the sum of multiple of a upto N. Examples : Input : a = 4, N = 23 Output : sum = 60 [Multiples : 4, 8, 12, 16, 20] Input :a = 7, N = 49 Output :sum = 196 [Multiples: 7, 14, 21, 28, 35, 42, 49] The basic idea is to iterate from i = a to i = n, i++ and check whether 5 min read Sum of the multiples of two numbers below N Given three integer A, B and N. The task is to find the sum of all the elements below N which are multiples of either A or B. Examples: Input: N = 10, A = 3, B = 5 Output: 23 3, 5, 6 and 9 are the only numbers below 10 which are multiples of either 3 or 5 Input: N = 50, A = 8, B = 15 Output: 258 Nai 12 min read Check if sum of divisors of two numbers are same Given two numbers n1 & n2, We need to check whether these numbers are equivalent numbers or not. Equivalent numbers are numbers such that the sums of their proper divisors are the same. For example, 159, 559, and 703 are equivalent numbers. This is because all three numbers have 57 as the sum of 5 min read How to compute mod of a big number? Given a big number 'num' represented as string and an integer x, find value of "num % a" or "num mod a". Output is expected as an integer. Examples : Input: num = "12316767678678", a = 10 Output: num (mod a) ? 8 The idea is to process all digits one by one and use the property that xy (mod a) ? ((x 4 min read Find sum of modulo K of first N natural number Given two integer N ans K, the task is to find sum of modulo K of first N natural numbers i.e 1%K + 2%K + ..... + N%K. Examples : Input : N = 10 and K = 2. Output : 5 Sum = 1%2 + 2%2 + 3%2 + 4%2 + 5%2 + 6%2 + 7%2 + 8%2 + 9%2 + 10%2 = 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 = 5.Recommended PracticeReve 9 min read Sum of the natural numbers (up to N) whose modulo with K yield R Given three integers N, K, and R. The task is to calculate the sum of all those numbers from 1 to N which yields remainder R upon division by K.Examples: Input: N = 20, K = 4, R = 3 Output: 55 3, 7, 11, 15 and 19 are the only numbers that give 3 as the remainder on division with 4. 3 + 7 + 11 + 15 + 4 min read Count Number of Divisible Triplet Sums Given a 0-indexed integer array arr[] and an integer d, return the count number of triplets (i, j, k) such that i < j < k and (arr[i] + arr[j] + arr[k]) % d == 0. Example: Input: arr= [3,3,4,7,8], d = 5Output: 3Explanation: The triplets which are divisible by 5 are: (0, 1, 2), (0, 2, 4), (1, 2 14 min read Maximum subarray sum modulo m Given an array of n elements and an integer m. The task is to find the maximum value of the sum of its subarray modulo m i.e find the sum of each subarray mod m and print the maximum value of this modulo operation.Examples: Input: arr[] = {10, 7, 18}, m = 13Output: 12Explanation: All subarrays and t 7 min read Number of common digits present in two given numbers Given two positive numbers N and M, the task is to count the number of digits that are present in both N and M. Examples: Input: N = 748294, M = 34298156Output: 4Explanation: The digits that are present in both the numbers are {4, 8, 2, 9}. Therefore, the required count is 4. Input: N = 111222, M = 10 min read Like