0

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)

2
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Narue
0

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

0

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

0

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.

0

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.

0

>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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.