两个数的和以 M 为模
给定三个数字 A、B 和 M。任务是在模 M 下打印 A 和 B 的总和。
例子:
Input:
a = 10, b = 20, m = 3
Output:
0
Explanation:
10+20 = 30 % 3 = 0
Input:
a = 100, b = 13, m = 107
Output:
6
方法:
将两个给定的数字 A 和 B 相加,并在模 M 下打印它们的总和。
下面是上述方法的实现:
C++
// C++ program for sum of two
// numbers modulo M
#include
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
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
Javascript
输出
0