# Sum of two numbers modulo M in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this problem, we are given three numbers a, b, and M. our task is to create a program to find the sum of two numbers modulo M.

## Let’s take an example to understand the problem,

Input: a = 14 , b = 54, m = 7
Output: 5
Explanation: 14 + 54 = 68, 68 % 7 = 5

To solve this problem, we will simply add the numbers a and b. And then print the remainder of the sum when divided by M.

## Example

Program to illustrate the working of our solution,

Live Demo

#include <iostream>
using namespace std;
int moduloSum(int a, int b, int M) {
return (a + b) % M;
}
int main() {
int a = 35, b = 12, M = 7;
cout<<"The sum modulo is "<<moduloSum(a,b,M);
return 0;
}

## Output

The sum modulo is 5

Updated on 17-Aug-2020 09:40:20