Rob
0
Q:

modulo c++

if (iNum % 2 == 0) 
{
cout << num << " is even ";
}
// % is used to mod numbers 
1
#include <iostream>
using namespace std;

int main() {
	cout << 11%3; // Prints 2
}
0
// 	Mode(%) => to get remainder
	1 % 10 = 1
 	2 % 10 = 2
    10 % 10 = 0  
    
0
#include <bits/stdc++.h>
using namespace std;

int main() {
  	int MOD = 1e9+7; //MOD = 1000000007
  	int a = 1224839897; //let a be a large number
  	int b = 1343589784; //also b is a large number
  	//then if we perform multiplication, addition or subtraction
  	//between these integers then they might result to overflow
  	//to overcome this situation we use modulo with a prime number > 1billion
  	//alright lets perform those operations...
  	
 	//MULTIPLICATION
	int mul = (a*b) % MOD;
  	//ADDITION AND SUBTRACTION IS QUITE A TRICKY OPERATION WITH MODULO
  	//which goes like this...
	int add = (a % MOD + b % MOD) % MOD;
	int sub = (a % MOD - b % MOD + MOD) % MOD;
  	
  	cout << "Multiplication : " << mul << '\n';
  	cout << "Addition : " << add << '\n';
  	cout << "Subtraction : " << sub << '\n';
  	//In this way overflow can be handled in many cases.
  	return 0;
}

//Output...
Multiplication : 312325592
Addition : 568429667
Subtraction : 881250120

-2

New to Communities?

Join the community