By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Sum

P: 80
Hi guys i want to calculate the sum of 10 numbers using loop

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int num1=0;
  6. int num2=1;
  7. while(num2<10)
  8. {
  9. num1++;
  10. num2=num1+num2;
  11. }
  12. cout<<num2;
  13. }
  14.  
This code however not working ...i think i want to assign num2 too...
Feb 17 '07 #1
Share this Question
Share on Google+
12 Replies


100+
P: 1,646
What is it that is 'not working'?
Feb 17 '07 #2

P: 80
What is it that is 'not working'?

That code didnt add the first 10 numbers....
Feb 17 '07 #3

AdrianH
Expert 100+
P: 1,251
Hi guys i want to calculate the sum of 10 numbers using loop

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int num1=0;
  6. int num2=1;
  7. while(num2<10)
  8. {
  9. num1++;
  10. num2=num1+num2;
  11. }
  12. cout<<num2;
  13. }
  14.  
This code however not working ...i think i want to assign num2 too...
The problem is that you are testing num2 not num1 for the exit condition.


Adrian

PS. you shouldn't use #include<iostream.h> Use #include<iostream> and then use "using namespace std;" Don't use that in a header file though. Bad things may happen. The include you are including is antiquated and deprecated.
Feb 17 '07 #4

P: 80
The problem is that you are testing num2 not num1 for the exit condition.


Adrian

PS. you shouldn't use #include<iostream.h> Use #include<iostream> and then use "using namespace std;" Don't use that in a header file though. Bad things may happen. The include you are including is antiquated and deprecated.

I am using an old complier fstream alone will not work on that compiler so i am using.h...
And i didnt get that solution i submitted
i just want to find the sum of first 10 numbers...
Feb 18 '07 #5

AdrianH
Expert 100+
P: 1,251
I am using an old complier fstream alone will not work on that compiler so i am using.h...
Ok. Fair enough.
And i didnt get that solution i submitted
i just want to find the sum of first 10 numbers...
Yes, so you should exit on the count to 10, not the sum. Try using more descriptive variable names, it will help you in the future.


Adrian
Feb 19 '07 #6

P: 80
Ok. Fair enough.

Yes, so you should exit on the count to 10, not the sum. Try using more descriptive variable names, it will help you in the future.


Adrian

what do you mean by descriptive variable names..
did u please explain it...
Feb 22 '07 #7

100+
P: 1,646
what do you mean by descriptive variable names..
did u please explain it...
num1 and num2 do not describe the variable in a useful way. just like text1 or text2. When you come back to read your code after 2 weeks on the beach, none of it will make sense to you.

You don't need to use really long descriptions like:
int inputintegertogetinputnumberfromuser;
you can use
int InputValue; which will be easy to understand in six months time. Much easier than num1 or num2 don't you think?
Feb 22 '07 #8

P: 3
Plaese telll him about Hungarian and other notations. Let him start correct basic steps.
Feb 22 '07 #9

DeMan
100+
P: 1,806
try
http://en.wikipedia.org/wiki/Hungarian_notation
for Hungarian notation.

What other sort of notations did you want to know about?
Feb 22 '07 #10

P: 9
Hi guys i want to calculate the sum of 10 numbers using loop

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int num1=0;
  6. int num2=1;
  7. while(num2<10)
  8. {
  9. num1++;
  10. num2=num1+num2;
  11. }
  12. cout<<num2;
  13. }
  14.  
This code however not working ...i think i want to assign num2 too...

try this code


<code removed>
Feb 22 '07 #11

P: 3
Expand|Select|Wrap|Line Numbers
  1. Quote:
  2. Originally Posted by reon
  3. Hi guys i want to calculate the sum of 10 numbers using loop
  4.  
  5. Code:
  6.  
  7. #include<iostream.h>
  8. #include<conio.h>
  9. void main()
  10. {
  11. int num1=0;
  12. int num2=1;
  13. while(num2<10)
  14. {
  15. num1++;
  16. num2=num1+num2;
  17. }
  18. cout<<num2;
  19. }
  20.  
  21.  
  22.  
  23. This code however not working ...i think i want to assign num2 too... 
Your program runs very well without an error but i think you should be clear with what you wanna do. Your program <i am sure> will give you 11 as the answer. But you have not added 10 numbers as you, initially, wanted to do.
Feb 22 '07 #12

DeMan
100+
P: 1,806
I think you want to use num1 in the loop not num 2
I think you also want to initialise num1 to 1 and num2 to 0.....
Feb 22 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.