•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,668 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 130 | Replies: 3
![]() |
•
•
Join Date: May 2008
Posts: 29
Reputation:
Rep Power: 1
Solved Threads: 0
could someone please check this code and tell me where i went wrong... it always gives the result 1.00000 but as you can see in the for loop it shouldn't be like that.
#include <cstdio>
int main(void)
{
int n;
float s = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
if(i % 2 == 0) s -= 1/(i*i);
else s += 1/(i*i);
}
printf("%1.5f", s);
scanf("\n");
return 0;
}•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation:
Rep Power: 11
Solved Threads: 184
Typecasting worked for me
if(i % 2 == 0) s -= 1/(float)(i*i);
else s += 1/(float)(i*i); There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Windows XP is stuck! (Windows NT / 2000 / XP / 2003)
- Advertisment icons stuck to my desktop!!! (Viruses, Spyware and other Nasties)
- Stuck writing a Function (C++)
- error message when shutting down, startup page stuck to http://dr-search4u.com/ind... (Viruses, Spyware and other Nasties)
- Stuck In Restart Loop (Windows NT / 2000 / XP / 2003)
- Help with Class, stuck. (C++)
- CPU use stuck at 100% (Viruses, Spyware and other Nasties)
- Stuck on start up (Windows NT / 2000 / XP / 2003)
- Internet explorer 5 gets stuck (Web Browsers)
- stuck in lower case, lost functions (Windows NT / 2000 / XP / 2003)
Other Threads in the C++ Forum
- Previous Thread: Interesting Problem
- Next Thread: get user input for file name to open?



Linear Mode