# for loop help

Page 1 of 1

## 3 Replies - 614 Views - Last Post: 02 March 2013 - 01:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314018&amp;s=3249601f03d75f59f1ac4b96e5589843&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 glr

Reputation: -2
• Posts: 20
• Joined: 20-February 13

# for loop help

Posted 02 March 2013 - 12:24 AM

I just started learning loops in java and given this exercise: Suppose that the tuition for a university is \$40,000 this year and tuition increases 3% every year. Write a program that uses a for loop to compute the tution in 10 years.

```public class Exercise2{
public static void main(String[] args){
int sum=0;
int fee;

//Prints 10 statements
for(int i=1; i<11; i++){
for(fee=1; fee<11; fee=(40000*(3/100)))  //Calculates the fees
fee++;
System.out.println("The tution fee for year "+i+" is "+fee); //Results

}

}
}
```

This is my code so far and now I'm stuck on how to use the for loop to calculate the fee each year. Help?

Its not working correctly I need help

Is This A Good Question/Topic? 0

## Replies To: for loop help

### #2 Hishermen

Reputation: 2
• Posts: 4
• Joined: 01-March 13

## Re: for loop help

Posted 02 March 2013 - 12:35 AM

```int tuition = 40000;
for(int i = 1; i < 11, i++)
{
tuition = tuition + (tuition * .03);
System.out.println("Tuition for year" + i + "is" + tuition;
}

```

In this code it goes threw 10 times. Each time it sets the tuition to .03% higher than what is was previously. Hope this is what you were looking for.

### #3 Limecore

Reputation: 18
• Posts: 92
• Joined: 28-November 12

## Re: for loop help

Posted 02 March 2013 - 12:42 AM

You screwed up the 2nd loop
it would (ignoring the fact that it would stop after the first iteration because 4000083/100 is greater than 11)
first make fee = 1, then 2, then 4000083/100, then 4000083/100+1, then 4000083/100+1, then 4000083/100 + 1
I think you may be able to do it with one loop

```...
for(double i=1, fee = 40000; i<=10;i++,fee=fee*1.03){
System.out.println("The fee for year " + i + " is \$"+ fee);
}
```

### #4 glr

Reputation: -2
• Posts: 20
• Joined: 20-February 13

## Re: for loop help

Posted 02 March 2013 - 01:06 AM

I have this way that I use to format double values to 2 decimal places but its not working in the loops. why?

```tuition=(int)(tuition*100)/100.0;
```

Nvm its working just wrong placement. Thanks