
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sum of two numbers modulo M in C++
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,
#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
- Related Articles
- Maximum subarray sum modulo m in C++
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N in C++
- Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
- Program to find the maximum sum of the subarray modulo by m in Python
- Sum of the natural numbers (up to N) whose modulo with K yield R in C++
- Sum of two large numbers in C++
- The sum of two consecutive numbers are 45 find the numbers.
- Sum of the multiples of two numbers below N in C++
- Check if sum of divisors of two numbers are same in Python
- Sum of even numbers from n to m regardless if nm JavaScript
- What is the sum of any two (a) Odd numbers?(b) Even numbers?
- The sum of two numbers is 16. The sum of their reciprocals is $\frac{1}{3}$. Find the numbers.
- The sum of two numbers is 18. The sum of their reciprocals is $\frac{1}{4}$. Find the numbers.
- The sum of two numbers is 9. The sum of their reciprocals is $\frac{1}{2}$. Find the numbers.
- Finding sum of first and last digit using divide and modulo operator in C language

Advertisements