-1

I want to know the mod function. It's like we've been searching for years after using the mod function in Excel. Can we do the same in c++?

For example, when mod in Excel, Id = 199734902138 = mod(id,100000000) As the answer, 34902138 Then id - 34902138 As the answer, 199700000000 Then 199700000000/100000000 Then we can get as the answer 1997 This is the year 1997

How to do the same thing in c++ using mod as mentioned above? I want to know that. Can you please help with that?

THAVIDU
  • 11
  • 2
  • Exactly the same way. But it’s much easier to just divide, there’s no need for modulo and subtraction – Sami Kuhmonen Sep 24 '22 at 04:29
  • How to do that? can you please send me the code? I need it as I said earlier. – THAVIDU Sep 24 '22 at 04:34
  • You literally do the division you showed in your question and that’s it – Sami Kuhmonen Sep 24 '22 at 04:34
  • #include #include using namespace std; int main(int argc, char** argv) { int id; int year; int mod; cout << "Enter Your ID Number : "; cin >> id; year = mod (id/10000000); cout << year; return 0; } – THAVIDU Sep 24 '22 at 04:38
  • Here is the code. – THAVIDU Sep 24 '22 at 04:39
  • mod function is not working. I need to know about how it work? – THAVIDU Sep 24 '22 at 04:40
  • `auto stupidTime = 199734902138; auto year = stupidTime / 100000000;` – paddy Sep 24 '22 at 04:44
  • I want to extract 1997 from 199734902138 using mod function. – THAVIDU Sep 24 '22 at 04:53
  • As we keep saying, you do _not_ need to do that because integer division will do it for you. You could subtract the modulo first, but that's completely and utterly pointless _unless_ you are using floating-point values. And whether or not that's the case is currently anybody's guess. – paddy Sep 24 '22 at 05:01

2 Answers2

2

In C++, % is modulo operator, like

long int ID = 199734902138;
long int m = ID % 100000000; // results 34902138
int year = (ID - m) / 100000000; // results 1977

But a simple division does the same thing in C++, because an integer divided by an integer results another integer

int year = 199734902138 / 100000000; // results 1977
sxu
  • 151
  • 5
0

Modulo doesn't find year, it returns the remainder after a division.

The modulo operator is %.

For example:

#include <iostream>

int main() {

    int x;
    x = 10 % 8; 

    std::cout << x << std::endl; // output is 2
    return 0;
}

Given your example, the following code would perform the same order of operations as your question. Notice the use of the long long int data type. Values this high (12-digit numbers) can only be expressed using long long int type.

#include <iostream>

int main() {

// declare variable id = 199734902138 and initial answer
long long int id = 199734902138;
long long int answer = id % 100000000;

// answer is now 199700000000 
answer = id - answer;

//final calculation, divide the answer by 100000000
id = answer / 100000000;

// output id for verification 
std::cout << id <<std::endl;

return 0;

}

As mentioned, this is all a bit superfluous as a simple divide operation will yield the same result, however if these steps need to be explicitly used in your calculation, then the code above would fit.

Alkurion
  • 26
  • 4