• Post Reply Bookmark Topic Watch Topic
  • New Topic

cant count totals in a for loop  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i have a loop counting from 1 to what ever number the user types in



but for the life of me i cant work out how to get a total of the numbers the computer is counting up to...
i did the same exercise in a while loop...had no problem
i tried adding a total variable and doing a few things in the loop,but it only generates errors (as i thought it would)
i also tried total = i+i but this is wrong too

any ideas? just tell me where i should be looking...
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you meant total of numbers from 1 to user enetered number. Ex. user entered no = 5 then 1 + 2 + 3 + 4 + 5 = 15 you want to print.
Then you have to initialize total to 0 because total is a local variable they need to be initialized before using them.
In that for loop you can add total += i; which means total = total + i; It will add sum of all numbers in total and print total after for loop.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the loop itself or in the body?
thanks for the help...
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it either or both ways and see what happens.   You won't break anything by trying and you will learn a little more about how computer programs work.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:in the loop itself or in the body?
Did you try it either or both ways? If yes was the output correct? Or got error, If error then what that error says? please let us know so we can understand the status of your problem...
 
Hardik Jani
Greenhorn
Posts: 7
Android AngularJS PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do something like below or directly use below code:

Sample example - 1:

public static void main(String[] args) {
        int n = 10;
        int sum = 0;
        for(int e = 1; e <=n; e++)         
            sum = sum + e;
        System.out.println(sum);
    }


Sample Example - 2:

int sum = 0;     // 1 time
int i = 0;       // 1 time
while (i < n) {  // n+1 times
  sum++;         // n times
  i++;           // n times
}


Sample Example - 3:

int counter = 1

for(int i=1;i<20;i++) {
if (i%2==0) //Match for 2
{
  counter+=1;
}
}
System.out.print(counter);



Hope, this can helpful to you.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Hardik!  Please post your code using code tags. Click here -->Where is code tag to know how.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You almost have it; you just need to increment total every iteration and display total after the loop is finished.

To increment the total, you can do that as part of the For loop by adding i to it and then incrementing i:

which is functionally the same as:


After your For loop's closing brace, display the total:
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and be sure to initialise total:

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are all right...i will never be afraid to try code in different places...i mean come on...im not exactly writing banking software here lol!!!

thanks for all the input

will let you know how i get on.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:im not exactly writing banking software here lol!!!

Even people who write banking software try stuff, test it, change it, try something else, test it some more...
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok got it...thanks for the help...really appreciate it
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!