+ 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
7 Antworten
+ 3
Yeah, I was busy at the time so I read it really quickly ... sorry :/
+ 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.
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;
}
0
i had to convert to double
0
it worked
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
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