| | |
stuck
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 29
Reputation:
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.
C++ Syntax (Toggle Plain Text)
#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; }
Typecasting worked for me
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Windows XP is stuck! (Windows NT / 2000 / XP)
- 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)
- Help with Class, stuck. (C++)
- CPU use stuck at 100% (Viruses, Spyware and other Nasties)
- Stuck on start up (Windows NT / 2000 / XP)
- Internet explorer 5 gets stuck (Web Browsers)
- stuck in lower case, lost functions (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Interesting Problem
- Next Thread: get user input for file name to open?
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






