Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
Reverse asterisks right triangle, java program

I am very close to this, however instead of the asterisks being horizontal, in a row, they are vertical, one in top of the other.

Question: Write a program that displays the output below. Use a variable to store the value for the number of asterisks in the first row. You must use multiple loops in your solution.

**********
*********
********
*******
******
*****
****
***
**
*

I currently have:

public class JavaAssignment6b {
    public static void main( String[] args ) {
        for(int r = 1; r <= 10; r++) {
            for(int j = 10; j >= r; j--) {
                System.out.println("*");
            }
            System.out.println(" ");
        }
    }
}               
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Simple answer to this question. All you have to do is add a simple if statement. I have posted two solutions below, one prints the number of asterisks on the first line, the other posts the number of asterisks on every line.

Solution 1

To print the number of asterisks on the first line:

int numberOfAsterisks = 0;
    for(int r = 1; r <= 10; r++) {
        for(int j = 10; j >= r; j--) {
            System.out.print("*");
            numberOfAsterisks++;
         }    
         if (r==1) {
            System.out.print(numberOfAsterisks);
         }
            System.out.println();
    }               
}

And the output

**********10
*********
********
*******
******
*****
****
***
**
*

Solution 2

To print the number of asterisks on every line:

int numberOfAsterisks = 0;
                for(int r = 1; r <= 10; r++) {
                    for(int j = 10; j >= r; j--) {
                        System.out.print("*");
                        numberOfAsterisks++;
                    }
                    System.out.print(numberOfAsterisks);
                    numberOfAsterisks = 0;
                    System.out.println();
        }               
    }

and the output for this answer:

**********10
*********9
********8
*******7
******6
*****5
****4
***3
**2
*1
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!