944,010 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 681
  • C++ RSS
Aug 20th, 2007
0

confirmation of LOOP

Expand Post »
hey guyz!!! i'm just trying to check my code u all if it's correct. thankz!

the problem is " ask a number (N). calculate and display the value of x, where x = (1*N)+(2*N)+(3*N)...(N*N);
ex. if i input a number 5 the formula of x where: x = (1*N)+(2*N)+(3*N)+(4*N)+(5*N);

this is my code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. int main ()
  3. {
  4. int n,x,y;
  5. y=0;
  6. cout<<"\nEnter number: ";
  7. cin>>n;
  8.  
  9. while (y<=n)
  10. {
  11.  
  12. cout<<"\n"<<y;
  13. y++;
  14. x += (y*n);
  15.  
  16. }
  17. cout<<"\n\nx = "<<x;
  18.  
  19. return 0;
  20. }
Last edited by Ancient Dragon; Aug 20th, 2007 at 10:12 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hoceandress is offline Offline
12 posts
since Aug 2007
Aug 20th, 2007
0

Re: confirmation of LOOP

>>i'm just trying to check my code u all if it's correct
does it do what its supposed to do ? If yes, then its ok. If not then you need to fix it. One problem though is that your program will produce data overflow errors if the value of n is too large.
Last edited by Ancient Dragon; Aug 20th, 2007 at 10:15 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 20th, 2007
0

Re: confirmation of LOOP

not! the display is different with my calculation on my own..
x += (y*n); << i'm not sure with this formula...
can you help me with this?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hoceandress is offline Offline
12 posts
since Aug 2007
Aug 20th, 2007
0

Re: confirmation of LOOP

The value of x is uninitialized, and the value of y should start at 1 not 0. It might be better to use a for loop instead of a while loop.
Last edited by Ancient Dragon; Aug 20th, 2007 at 10:31 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 20th, 2007
0

Re: confirmation of LOOP

NICE ONE MAN!! I GOT IT!! THANKS, MORE POWER TO YOU...

i'm using the "for loop" its results, like my calculation...huh!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hoceandress is offline Offline
12 posts
since Aug 2007
Aug 20th, 2007
0

Re: confirmation of LOOP

(1*n)+(2*n)+(3*n)...(n*n) == (1+2+3+..+n)*n == (n+1)/2*n*n
Last edited by vijayan121; Aug 20th, 2007 at 10:57 pm.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: syntax error before '::' token
Next Thread in C++ Forum Timeline: C++ and MySql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC