473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sum

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
12 1859
willakawill
1,646 1GB
What is it that is 'not working'?
Feb 17 '07 #2
reon
80
What is it that is 'not working'?

That code didnt add the first 10 numbers....
Feb 17 '07 #3
AdrianH
1,251 Expert 1GB
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
reon
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
1,251 Expert 1GB
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
reon
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
willakawill
1,646 1GB
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
rajgkk
3
Plaese telll him about Hungarian and other notations. Let him start correct basic steps.
Feb 22 '07 #9
DeMan
1,806 1GB
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
keyu
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
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
1,806 1GB
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

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

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.