Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDylan Fried
3,123 PointsNested for loop multiplication table help
The output has to be:
9*9= 81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9
8*8=64 8*7=56. . .
this will decrease until it reaches 1*1=1
I'm having trouble in decreasing the second number.
public class $1_8{
public static void main(String[] args){
int num1 = 9, num2 = 9;
for(int i = 9; i >= 1; i--){
for(int n = 1; n <=i; n++){
int sum = num1 * num2;
System.out.println(num1 + "*" + num2 + "=" + sum + "\t");
}
num1--;
System.out.println();
}
}
}
1 Answer
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsYou have two variables num1
and num2
for the multipliers, and also two variables i
and n
and you're using to keep track of how many times you've gone through a loop. I suggest just using num1 and num2 for both purposes.
public static void main(String[] args){
for(int num1 = 9; num1 >= 1; num1--){
for(int num2 = 9; num2 >= 1; num2--){
int sum = num1 * num2;
System.out.println(num1 + "*" + num2 + "=" + sum + "\t");
}
num1--;
System.out.println();
}
}