Hi everyone,

I need help with the C++ program. I need to find a way to write this:

compute the product of a*(a+1)*(a+2)*...*(b)

Recommended Answers

All 5 Replies

What have you tried? It looks like a fairly simple sequence that would be translated into an equally simple loop.

i tried:
if (a<b)
{
for (int a<=b; a++)
{
result=result*a;
cout <<result<<endl;
}
}

Yea, that's not even a complete program, much less a correct one. This actually works:

#include <iostream>

using namespace std;

int main()
{
  int a = 1;
  int b = 5;
  int result = 1;

  if (a<b)
  {
    for (; a<=b; a++)
    {
      result=result*a;
      cout <<result<<endl;
    }
  }
}

Note that a for loop has three clauses: initialization, which happens when the loop first begins and never happens again; conditional, which drives the loop and occurs at each iteration; increment, which happens at the end of each iteration. These clauses are always separated by a semicolon, even if the clause is empty. And all of the clauses are optional.

yeah i wrote the whole program but with
int a;
int b;
int result;
without putting values, but it didnt work, i dont know why.
anyway thank you.

>without putting values, but it didnt work, i dont know why.
The initial value for a local variable will be random unless you specify one.

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.