Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

310
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda