+ 2

Help me with c++

well, i wantend to test values for b, from 1 to 10 whether they are even or odd and then, for each case, perform different procedures {S-=b/(b*b)} if the number is even and {S+=b/(b*b)} if it's odd. Finally, i wanted to print the results for each value of b tested for these conditions https://code.sololearn.com/ciK50d1hXCKM/?ref=app

25th Aug 2017, 2:16 PM
Henrique Sued
Henrique Sued - avatar
7 Respostas
+ 3
Yeah, I was busy at the time so I read it really quickly ... sorry :/
25th Aug 2017, 5:42 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Your code seems correct. The only improvement I see is removing the second if, keeping only the else as a number can not be neither even nor odd.
25th Aug 2017, 2:40 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
include <iostream> using namespace std; int main() { double S; int b=1; for (b=1;b<=10;b++){ if (b%2!=0){ S=(-(double)b/((double)b*(double)b)); } else { S=((double)b/((double)b*(double)b)); } cout<<"Seu numero S="<<S<<endl; } return 0; }
26th Aug 2017, 2:48 PM
Henrique Sued
Henrique Sued - avatar
0
i had to convert to double
26th Aug 2017, 2:48 PM
Henrique Sued
Henrique Sued - avatar
0
it worked
26th Aug 2017, 2:49 PM
Henrique Sued
Henrique Sued - avatar
0
Seu numero S=-1 Seu numero S=0.5 Seu numero S=-0.333333 Seu numero S=0.25 Seu numero S=-0.2 Seu numero S=0.166667 Seu numero S=-0.142857 Seu numero S=0.125 Seu numero S=-0.111111 Seu numero S=0.1
26th Aug 2017, 2:49 PM
Henrique Sued
Henrique Sued - avatar
0
idk, the whole thing is a list of exercises from my teacher. we're being introduced to programming. these lines of code are very simple i know, but thats probably what he intented it to be
27th Aug 2017, 5:12 AM
Henrique Sued
Henrique Sued - avatar