adding sequence

#include <iostream>
using namespace std;

int main()
{
float i, n, sum = 0;

cout << " Enter n ";
cin >> n;

for (i = 1; i <= n; i++)
{

cout << (1+((i-1)/(2*(i-1)+1)))*(1+(i/(2*i+1))) << endl;
}

}
Result=1.33333
1.86667
2
correct result=1.33333
1.46667
1.52381
this seems only to be precise with n=1 only.
The condition is I cannot use setprecision too
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

Are you sure that this equation of yours is correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
  int n = 5;
  for ( int i = 1; i <= n; ++i )
  {
    const auto im = i-1;
    const auto lhs = 1 + im / (2.0 * im + 1);
    const auto rhs = 1 + i  / (2.0 * i  + 1); 
    std::cout << lhs << "*" << rhs << " \t= " << lhs  * rhs << '\n';
  }
}

1*1.33333 	= 1.33333
1.33333*1.4 	= 1.86667
1.4*1.42857 	= 2
1.42857*1.44444 	= 2.06349
1.44444*1.45455 	= 2.10101
Are you sure it's not the sequence in this thread?
http://www.cplusplus.com/forum/beginner/231569/#msg1044944

Input n ( > 0 ):6
p(1) = 1.33333
p(2) = 1.46667
p(3) = 1.52381
p(4) = 1.54921
p(5) = 1.56075
p(6) = 1.56608


which, as @Keskiverto pointed out, is definitely not the equation that you have posted.
Last edited on
I think it should be like this
#include <iostream>

int main()
{
int n = 5;
for ( int i = 1; i <= n; ++i )
{
const auto im = i-1;
const auto lhs = 1 + im / (2.0 * im + 1);
const auto rhs = 1 + i / (2.0 * i - 1);
std::cout << lhs << "*" << rhs << " \t= " << lhs * rhs << '\n';
}
}
How do you know the "correct" answers?
it was a practice problem with answers
Topic archived. No new replies allowed.