May 8, 2014 at 4:17pm UTC
You cannot use straight mod operation on doubles. Why n is double? Would int suffice?
May 8, 2014 at 4:22pm UTC
my target:
1+3/2+5/4+7/6+...+(n+1)/n
May 8, 2014 at 4:25pm UTC
But you are not dividing anything by n.
May 8, 2014 at 5:02pm UTC
Without condition, works great
but i want to use condition
May 8, 2014 at 5:48pm UTC
CHANGE. TYPE. OF. n. TO. int.
Remove if in the else statement at line 15.
Here is code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include<conio.h>
#include<iostream.h>
void main()
{
double s=1,i; int n;
clrscr();
cout<<"Enter n(n=2k):" ;
cin>>n;
if (n%2>0)
{
cout<<"the number you entered is odd" ;
cin>>n;
}
else if (n%2!=0)
{
for (i=2;i<=n;i+=2)
{
s+=(i+1)/i;
}
cout<<"sum of them=" <<s;
}
getch();
}
Enter n(n=2k):8
sum of them=6.04167
Proof:
http://ideone.com/YHU5SU
Had to change code a little to make it compile on something other than ancient non-standard compilers.
Last edited on May 8, 2014 at 6:02pm UTC
May 9, 2014 at 4:17am UTC
I tried your code.
But when I entered odd number
message (the number you entered is odd) not found
May 9, 2014 at 7:32am UTC
WHy you are using code from ideone when it was changed to work on modern compilers?
I posted code you need in my post earlier.
May 9, 2014 at 8:11am UTC
Compiler problems. Code should work, but buggy compiler cannot correctly link it.
You should really think about upgrading: you are using headers which were discontinued in 1998. After that another 3 4 times language was updated.
May 9, 2014 at 8:16am UTC
Can you give me download link of the latest version of c++ for Windows 8???