Write a function to compute and return the value of the sum. Assume that the value
of n is an argument of the function.
s = 1 + 3 + 5 + . . . + (2n − 1)
Then in main function use a while loop to read values of the parameter n and print the sum with
each iteration.
This is a bit confusing for me. Please help. Thank you in advance. This is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "std_lib_facilities.h"
s=0;
for(int i=1;i<2*n;I+=2)
{
s+=i;
int main {
while( )
{
//Some code here
cout << "s = 1 + " << /something
}
return 0;
}
#include "std_lib_facilities.h"
int main ()
{
int sum = 0;
int num=1;
int a;
cout << "Enter a value for a: ";
cin >> a;
while (num<=a)
{
sum+= 2*num-1;
if(num!=a)
cout << 2*num - 1 << " + ";
else
cout << 2*num -1;
num++;
}
cout << " = " << sum << endl;
return 0;
}
This is the updated code and it runs the way I want it to. However I feel like in the directions that I am given I also need something outside the main function. How can I go about that from here?
Beginning to think it's not recursive function. Blanking out on the name of it though. Any information that I am given is below. I know nothing more and nothing less of this. I'll be honest and say the way my professor explained this really threw me off in class and I'm just trying to get a better understanding of it all.
Write a function to compute and return the value of the sum. Assume that the value
of n is an argument of the function.
s = 1 + 3 + 5 + . . . + (2n − 1)
Then in main function use a while loop to read values of the parameter n and print the sum with each iteration.