Notes: You must use a nested loop. There are spaces at the beginning of each row.
2 - 1 = 1
3 - 1 = 2 3 - 2 = 1
4 - 1 = 3 4 - 2 = 2 4 - 3 = 1
5 - 1 = 4 5 - 2 = 3 5 - 3 = 2 5 - 4 = 1
6 - 1 = 5 6 - 2 = 4 6 - 3 = 3 6 - 4 = 2 6 - 5 = 1
7 - 1 = 6 7 - 2 = 5 7 - 3 = 4 7 - 4 = 3 7 - 5 = 2 7 - 6 = 1
8 - 1 = 7 8 - 2 = 6 8 - 3 = 5 8 - 4 = 4 8 - 5 = 3 8 - 6 = 2 8 - 7 = 1
9 - 1 = 8 9 - 2 = 7 9 - 3 = 6 9 - 4 = 5 9 - 5 = 4 9 - 6 = 3 9 - 7 = 2 9 - 8 = 1
//Create class SubstractionTable.java
public class SubstractionTable {
void printSubstractionTable()
{
//nested loop to display substraction table
int rows=10;
for(int i=2;i<rows;i++)
{
for (int j =1; j < rows-i; j++) {
System.out.print(" ");
}
for(int j=1;j<i;j++)
{
System.out.print(i+"-"+j+"="+(i-j)+" ");//print the
table
}
System.out.println(" ");//new line after completion of inner
loop
}
}
public static void main(String[] args) {
SubstractionTable sc=new SubstractionTable();
sc.printSubstractionTable();
}
}
__________________________________________OUTPUT_________________________________________
Get Answers For Free
Most questions answered within 1 hours.