I'm trying to write a code which will generate numbers for a binomial coefficient from a number interval [2-10], i think it should look like this:
n=2
r=2
n=2
r=3
n=2
r=4
n=2
r=5
.
.
.
n=10
r=10
-The program should stop here.
This is badly written, but it still has the basic idea in it.
Can anyone help me?
#include <iostream>
using namespace std;
int main () {
int n=2;
int r=1;
while (n!=10 && r==10) {
n++;
cout << n << endl;
}
while(r<10 && n!=10) {
r++;
cout << r << endl;
if (r==10) {
r-=9;
}
}
cout << n <<endl;
cout << r <<endl;
return 0;
}
When posting code here, please use code formatting tags, to make it easier to read. Use the '<>' button to the right of the editing window.
Your first while loop will never execute, because when you first reach it, r is 1, not 10. So n will not be incremented, and will remain equal to 2.
In your second loop, on the 9th iteration, r starts as 9, is incremented to 10 but then immediately you subtract 9 from it, so it goes back to 1. Since n is 2 (and not 10), it will loop infinitely, because r < 10 is always true.