hello!
i am a brand new user to this site ihope that my experence with this site would be better than previous ones, i am studying BS(computer science), recently i had trouble with a program that ihave made well that thing is that its is complete and working as well but there are problems with it i am sending the program as .cpp file i hope people at dani web will help me in correcting the program most of the problem is with the part where there is modulus and factorial function. :?:

Recommended Answers

All 3 Replies

What are you trying to do with those two functions? It's clearly not a simple factorial or modulo calculation, so unless you describe what the problem is, we can't help. Though I can tell you right now that your power function isn't going to work like you expect.

>i=a^b;
^ is the bitwise exclusive OR operator, not the power operator. C++ doesn't have such an operator.

can anyone tell me what is the syntx for power in c++ as well as factorial and modulus because i am at a begginers stage.

Power is a function declared in the <cmath> header:

#include <cmath>
#include <iostream>

using namespace std;

int main()
{
  cout<< pow ( 2, 4 ) <<endl;
}

Modulus does have an operator (%), but it only works for integer values. For floating-point, you need to use the fmod function declared in the <cmath> header.

N! is easy to calculate:

#include <iostream>

using namespace std;

int factorial ( int n )
{
  int r = n;

  while ( --n > 0 )
    r *= n;

  return r;
}

int main()
{
  cout<< factorial ( 5 ) <<endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.