I am required to compute all Lucas numbers up to a certain number. To keep track of a summation, more specifically, the summation of all Lucas numbers you have encountered up to that point.
A small example: If the user inputs a bound of 23: Lucas numbers up to 23 are: 2,1,3,4,7,11,18. Hence the sum is 2+1+3+4+7+11+18=46

Here's what I got:

else if (choice == 3) {
int a=2;
int b=1;
int c=3;
int bound, sum, i;
cout << "Plese enter the upper bound of the summation you are looking for:" << endl;
cin>>bound;
if (bound< 0) cout << "Only Posiive Numbers are accepted\n\n";

else{
for(i=1;c<=sum;i++)
{
a=b;
b=c;
c=a+b;
sum=sum+c;
}
cout << sum << " is the summation of all numbers until " << bound << endl;
} }

Recommended Answers

All 2 Replies

This is all off the top of my head, I didn't compile it or anything so it may need some fixing, but I'm pretty sure it'll work...


cout<<"Enter your boundary:"<<endl;

int bounds=cin.get();
int a,b;
bool first=0;

for(int i=1;i<bounds;i++){
if(first==0){
a=i;
first=1;
}
else{
b=i;
i=a+b;
cout<<i<<endl;
first=0;
}
}

I think that the only problem with your code is that the bound on the for-loop should be the variable "bound", not the variable "sum". You had this:

for(i = 1; c <= sum; i++)

I think you should have this instead:

for(i = 1; c <= bound; i++)
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.